我在我的.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