我在我的.NET应用程序中有一个上传附件页面。只要上传的文件小于4 MB,它就可以正常工作。如何设置上传文件的大小?我想将限制设置为小于8 MB。如果文件大于8 MB,上传过程将被确定。如何在.NET/C#中控制上传文件大小?
这是我在后端的代码。 File1是上传控制器。
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.Equals(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");
}
}
请参阅http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net – 2011-06-14 20:37:53