使用asp.net和C#上傳過程中檢查文件大小的最佳方法是什麼?我可以通過改變我的web.config上傳大文件而不會有任何問題。當文件上傳超過我允許的最大文件大小時,我的問題就出現了。如何檢查上傳文件大小
我已經研究過使用activex對象,但這不是跨瀏覽器兼容的,也不是解決方案的最佳答案。我需要它是跨瀏覽器兼容的,如果可能的話,並支持IE6(我知道你在想什麼!!但是,我的應用程序用戶中有80%是IE6,這不會很快就會很快改變)。
有沒有發現有同樣的問題嗎?如果是這樣,你是如何解決它的?
使用asp.net和C#上傳過程中檢查文件大小的最佳方法是什麼?我可以通過改變我的web.config上傳大文件而不會有任何問題。當文件上傳超過我允許的最大文件大小時,我的問題就出現了。如何檢查上傳文件大小
我已經研究過使用activex對象,但這不是跨瀏覽器兼容的,也不是解決方案的最佳答案。我需要它是跨瀏覽器兼容的,如果可能的話,並支持IE6(我知道你在想什麼!!但是,我的應用程序用戶中有80%是IE6,這不會很快就會很快改變)。
有沒有發現有同樣的問題嗎?如果是這樣,你是如何解決它的?
我們正在使用NeatUpload來上傳文件。
雖然這樣做的大小檢查後上傳,所以可能不符合您的要求,雖然它可以選擇使用SWFUPLOAD上傳文件和檢查大小等,可以設置的選項,使它不'不要使用這個組件。
由於他們發回回傳處理程序的方式,還可以顯示上傳的進度欄。如果使用內容大小屬性的文件大小超過了所需的大小,您也可以在處理程序的早期拒絕上載。
如果您正在使用System.Web.UI.WebControls.FileUpload
控制:
MyFileUploadControl.PostedFile.ContentLength;
返回發佈文件的大小,以字節爲單位。
這是我在上傳文件時做的,它可能對您有幫助嗎?我會檢查文件大小等。
//did the user upload any file?
if (FileUpload1.HasFile)
{
//Get the name of the file
string fileName = FileUpload1.FileName;
//Does the file already exist?
if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["fileUploadPath"].ToString() + fileName)))
{
PanelError.Visible = true;
lblError.Text = "A file with the name <b>" + fileName + "</b> already exists on the server.";
return;
}
//Is the file too big to upload?
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize > (maxFileSize * 1024))
{
PanelError.Visible = true;
lblError.Text = "Filesize of image is too large. Maximum file size permitted is " + maxFileSize + "KB";
return;
}
//check that the file is of the permitted file type
string fileExtension = Path.GetExtension(fileName);
fileExtension = fileExtension.ToLower();
string[] acceptedFileTypes = new string[7];
acceptedFileTypes[0] = ".pdf";
acceptedFileTypes[1] = ".doc";
acceptedFileTypes[2] = ".docx";
acceptedFileTypes[3] = ".jpg";
acceptedFileTypes[4] = ".jpeg";
acceptedFileTypes[5] = ".gif";
acceptedFileTypes[6] = ".png";
bool acceptFile = false;
//should we accept the file?
for (int i = 0; i <= 6; i++)
{
if (fileExtension == acceptedFileTypes[i])
{
//accept the file, yay!
acceptFile = true;
}
}
if (!acceptFile)
{
PanelError.Visible = true;
lblError.Text = "The file you are trying to upload is not a permitted file type!";
return;
}
//upload the file onto the server
FileUpload1.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["fileUploadPath"].ToString() + fileName));
}`
您可以通過執行以下步驟來做到的Safari和FF只需
<input name='file' type='file'>
alert(file_field.files[0].fileSize)
你可以檢查在asp.net:
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
string savePath = @"c:\temp\uploads\";
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
// Get the size in bytes of the file to upload.
int fileSize = FileUpload1.PostedFile.ContentLength;
// Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
if (fileSize < 2100000)
{
// Append the name of the uploaded file to the path.
savePath += Server.HtmlEncode(FileUpload1.FileName);
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it exceeds the 2 MB size limit.";
}
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
網頁中添加這些行。配置文件。
正常文件上傳大小爲4MB。這裏在system.web
maxRequestLength
在KB中提到和在system.webServer
maxAllowedContentLength
在Bytes中。
<system.web>
.
.
.
<httpRuntime executionTimeout="3600" maxRequestLength="102400" useFullyQualifiedRedirectUrl="false" delayNotificationTimeout="60"/>
</system.web>
<system.webServer>
.
.
.
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1024000000" />
<fileExtensions allowUnlisted="true"></fileExtensions>
</requestFiltering>
</security>
</system.webServer>
,如果你想知道web.config
提到maxFile
上傳大小使用給定的行.cs
頁
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
//get Max upload size in MB
double maxFileSize = Math.Round(section.MaxRequestLength/1024.0, 1);
//get File size in MB
double fileSize = (FU_ReplyMail.PostedFile.ContentLength/1024)/1024.0;
if (fileSize > 25.0)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('File Size Exceeded than 25 MB.');", true);
return;
}
感謝您的意見。在嘗試了一些建議的解決方案之後,我最終使用了Teleriks RAD上傳組件,它允許我做我需要的。 – Cragly 2009-09-04 13:53:38
這可以通過Silverlight或Flash完成。對於Falsh,你可以看到[swfupload](http://www.swfupload.org/)。 – 2009-07-05 20:19:16
swfupload是免費的,很好。多次使用它,它真的回答了這個問題。 +1在這裏。 – 2009-07-05 21:48:35