上传时ASP.NET文件已损坏

时间:2017-06-29 14:44:28

标签: c# asp.net .net file-upload save-as

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

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

如果我上传文件大约200 KB或更少,一切顺利,但当我上传更大的内容,如10MB时,它们的长度会发生变化,当我下载它时,它就不再可用了。

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

我已更改maxRequestLength文件中的maxAllowedContentLengthweb.config值。 这是我上传文件的代码:

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是否有最大大小限制? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我道歉,我发现了自己的错误。我指定了错误的文件上传器,从中上传音频文件。所以我的代码所做的就是选择pdf文件,为其提供音频文件的名称和扩展名,并将其存储为mp3文件。我更改了文件上传者的名字,现在它可以正常工作了。谢谢你的建议!