2011-05-21 13 views
1

有人能告訴我這段代碼有什麼問題嗎?我試圖與上傳這段代碼的mp3文件,當我嘗試這樣做,我收到了「Internet Explorer無法顯示該網頁」無法使用C#上傳mp3,請幫忙!

<asp:FileUpload ID="FileUpload1" runat="server" /> 

<asp:Button ID="upload" runat="server" Text="GO" OnClick="btn_Click" /><br /><br /> 
<asp:Label ID="Label1" runat="server"></asp:Label> 
<script runat="server"> 
protected void btn_Click(object sender, EventArgs e) 
{ 
    if (FileUpload1.HasFile) 
    { 
     string fileExt = 
      System.IO.Path.GetExtension(FileUpload1.FileName); 

     if (fileExt == ".mp3") 
     { 
      try 
      { 
       FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + 
        FileUpload1.FileName)); 
       Label1.Text = "File name: " + 
        FileUpload1.PostedFile.FileName + "<br>" + 
        FileUpload1.PostedFile.ContentLength + " kb<br>" + 
        "Content type: " + 
        FileUpload1.PostedFile.ContentType; 
      } 
      catch (Exception ex) 
      { 
       Label1.Text = "ERROR: " + ex.Message.ToString(); 
      } 
     } 
     else 
     { 
      Label1.Text = "Only .mp3 files allowed!"; 
     } 
    } 
    else 
    { 
     Label1.Text = "You have not specified a file."; 
    } 
} 



    </script> 
+0

它可以與任何其他文件(也許是一個小的MP3文件)?您是否嘗試將文本文件的擴展名更改爲.mp3並將其上傳,以查看它是否適用於非常小的尺寸? – IAmTimCorey 2011-05-21 00:56:26

+0

我試圖刪除擴展部分,它確實工作。我還沒有嘗試一個小MP3!你認爲尺碼很重要嗎? – jorame 2011-05-21 00:58:40

+1

在這種情況下它確實很重要:)。默認情況下它的4 MB。 – YetAnotherUser 2011-05-21 00:59:58

回答

6

使用以下在你的web.config增加HTTP的maxRequestLength - 它應該工作。默認情況下它限制在4MB。

<system.web> 
    <httpRuntime executionTimeout="240" maxRequestLength="20480" /> 
</system.web> 
相關問題