Thursday, February 21, 2008

Upload Video in asp.net

First of all, by default, asp.net set the size is 4mb for each form request...if u want to upload larger file, u need to change the setting in web.config.

adding
between .

maxRequestLength is the maximum size of each form request
requestLengthDiskThreshold is the buffer size in server memory

before i didn't set executionTimeout, so the asp.net kills every request takes longer than 110seconds. so every time i upload the file takes longer than 2mins , it will comes up a not found page.....
set executionTimeout to allow the request time how long u want in seconds.

this method suppose is not good in uploading large files, it will spend all the server memory during upload progress. but in my project, there is only one person to upload the file, so it will not affect too much. But if so many people upload like youtube, hv to set sth like httphandle.....but i hvn't learn this, i think quite hard. btw, there is some control developed by company in order to handle this situation like PowUpload and KUpload. but not free.

then just use the control of FileUpload of asp.net.
and code:

aspx source:



code behind:
private bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".rmvb":
return false;
default:
return true;
}
}
private void initialDataList()
{
string upFolder = MapPath("~/Video/VideoFiles/");
DirectoryInfo dir = new DirectoryInfo(upFolder);
DataList1.DataSource = dir.GetFiles();
DataList1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
initialDataList();
}
protected void btnAddVideo_Click(object sender, EventArgs e)
{
if (upVideo.HasFile)
{
if (CheckFileType(upVideo.FileName))
{
string filePath = "~/Video/VideoFiles/" + upVideo.FileName;
upVideo.SaveAs(MapPath(filePath));
}
}
initialDataList();
}

No comments: