繁体   English   中英

上传时ASP.NET文件损坏

[英]ASP.NET files get corrupted when uploaded

我在将文件上传到服务器时遇到问题。

在我的网页中,我有三个不同的文件上传Web控件,从中可以选择文件。 要将文件保存在服务器上,我正在使用FileUpload.PostedFile.SaveAs()方法。

如果我上传的文件大约200 KB或更少,那么一切正常,但是当我上传更大的文件(例如10MB)时,它们的长度会发生变化,并且当我下载文件时,它不再可用。

例如,当我上载40KB的html文件,200KB的pdf文件和10MB的mp3文件时,mp3文件将被截断,长度变为200KB。 经过一些调试后,我不确定它会在哪里发生,因为在使用SaveAs()方法之前,文件大小已经更改。

我已经更改了web.config文件中的maxRequestLengthmaxAllowedContentLength值。 这是我上传文件的代码:

string htmlFilename = "";
string pdfFilename = "";
string audioFilename = "";
string folder= "";

if (uploadHtml.PostedFile != null && uploadHtml.PostedFile.ContentLength    > 0)
   htmlFilename = Path.GetFileName(uploadHtml.PostedFile.FileName);
if (uploadPDF.PostedFile != null && uploadPDF.PostedFile.ContentLength > 0)
    pdfFilename = Path.GetFileName(uploadPDF.PostedFile.FileName);
if (uploadAudio.PostedFile != null && uploadAudio.PostedFile.ContentLength > 0)
   audioFilename = Path.GetFileName(uploadAudio.PostedFile.FileName);

if (htmlFilename != "" || pdfFilename != "" || audioFilename != "")
{
   folder= Server.MapPath("Data/" + txtTitoloStudio.Text);
   if (!Directory.Exists(cartella))
   {
       Directory.CreateDirectory(cartella);
   }
if (htmlFilename != "")
{
   string htmlSaveLocation = cartella + "/" + htmlFilename;
   uploadHtml.PostedFile.SaveAs(htmlSaveLocation);
}
if (pdfFilename != "")
{
   string pdfSaveLocation = cartella + "/" + pdfFilename;
   uploadPDF.PostedFile.SaveAs(pdfSaveLocation);
}
if (audioFilename != "")
{
   string audioSaveLocation = cartella + "/" + audioFilename;
   uploadPDF.PostedFile.SaveAs(audioSaveLocation);
}

我怎么解决这个问题? FileUpload是否有最大大小限制? 谢谢您的帮助。

对不起,我发现了我的错误。 我指定了错误的文件上传器,从该文件上传器上载了音频文件。 因此,我的代码所做的就是选择pdf文件,为其提供音频文件的名称和扩展名,并将其存储为mp3文件。 我更改了文件上传器的名称,现在可以使用了。 感谢您的建议!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM