I have an upload attachment page in my .NET application. It works fine as long as the uploaded file is smaller than 4 MB. How do I set the size of uploaded file? I'd like to set the limitation to be less than 8 MB. And if the file is larger than 8 MB, the upload process will be determined.
This is my code in the backend. File1 is the upload controler.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
string name=ViewState["UserName"].ToString();
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string extension = Path.GetExtension(File1.PostedFile.FileName);
if (extension == "")
return;
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Attachments/" + name));
FileInfo[] rgFiles = di.GetFiles("*.*");
foreach (FileInfo fi in rgFiles)
{
if (fi.Name.Equ开发者_运维问答als(fn))
{
ShowMessage(this, "This file name already exists, please check the list.");
return;
}
}
string SaveLocation = Server.MapPath("~/Attachments/" + name + "/" + fn);
try
{
File1.PostedFile.SaveAs(SaveLocation);
ShowMessage(this,"The file has been uploaded.");
}
catch (Exception ex)
{
ShowMessage(this,"Error" + ex.Message);
}
GetList();
}
else
{
ShowMessage(this,"Please choose one file");
}
}
<configuration>
<system.web>
<httpRuntime maxRequestLength="8000" />
</system.web>
</configuration>
You use the web.config to sort this. Specifically the maxRequestLength property.
See here.
You have to change the maxRequestLength: http://www.bloggingdeveloper.com/post/Limiting-the-File-Upload-Size-in-ASPNET.aspx
Here's an existing post with same question: How to increase the max upload file size in ASP.NET?
Unfortunately there is no easy way to go about this problem. I had a similar problem with the uploading of images and people who try to upload their uncompressed 24ft wide image of their daughters birthday party :)
The problem is you cant figure out the file size until the file is basically uploaded to the server via the post. Which will cause the server to take forever and a day to load and eventually time out which doesn't make for a user friendly page.
The only solution I could come up with was a flash file uploader which would compress the file on the client machine then send the compressed file to the site. So a 200mb image becomes a 100k file. There are some open source flash projects out there just have to google them. This is what websites like Facebook and Picasa do for uploading documents/images.
I have not tried looking for one but maybe HTML5 will offer something since it is basically replacing flash at some point in the near future.
精彩评论