2010-04-05 42 views
2

這裏很難展示ASP.NET的代碼,所以我會盡我所能來描述我的問題。ASP.NET網頁不可用

我有一個FileUploadControl和一個按鈕,當它被點擊時調用一個函數。當沒有爲我的FileUploadControl選擇任何內容時,Button函數似乎工作。但是,當FileUploadControl中選擇了某些內容(我選擇了要上傳的文件)時,單擊該按鈕時出現問題。它的功能完全沒有關係(它可能只是寫入標籤,即使它與FileUploadControl無關)。我得到的錯誤是:

此網頁不可用。

http://localhost:2134/UploadMedia/Default.aspx的網頁可能會暫時關閉,或者它可能已永久移動到新的網址。

我在Google上搜索過,人們似乎遇到了這個問題,但是與我不同的原因。他們說他們的ASP.NET Development Server端口實際上與他們在地址欄中的端口不同。對我來說情況並非如此。

另外,人們遇到的另一個問題是Use Dynamic Ports。我試過truefalse。我也嘗試過不同的端口,而且我總是得到同樣的錯誤。

這真的讓我發瘋,因爲buttonFunction中的代碼並不重要,只要FileUploadControl中有某些東西,它就不起作用。如果什麼都沒有,它似乎工作得很好。

下面是ASP.NET控件的代碼:

<asp:FileUpload id="FileUploadControl" runat="server" /> 
<asp:Button runat="server" id="UploadButton" text="Upload" OnClick="uploadClicked" /> 
<br /><br /> 
<asp:Label runat="server" id="StatusLabel" text="Upload status: " /> 

這是該按鈕的功能代碼:

protected void uploadClicked(object sender, EventArgs e) 
{ 
    if (FileUploadControl.HasFile) 
    { 
     string filename = Path.GetFileName(FileUploadControl.FileName); 

     //Check if the entered username already exists in the database. 
     String sqlDupStmt = "Select songPath from Songs where songPath ='" + Server.MapPath("~/Uploads/") + filename + "'"; 
     SqlConnection sqlDupConn = new SqlConnection(@"Data Source = .\SQLEXPRESS; AttachDbFilename = |DataDirectory|\Database.mdf; Integrated Security = True; User Instance = True;"); 
     SqlCommand sqlDupCmd = new SqlCommand(sqlDupStmt, sqlDupConn); 
     sqlDupCmd.Connection.Open(); 
     SqlDataReader sqlDupReader = sqlDupCmd.ExecuteReader(CommandBehavior.CloseConnection); 

     if (sqlDupReader.Read()) 
     { 
      StatusLabel.Text = "Upload status: The file already exists."; 
      sqlDupReader.Close(); 
     } 

     else 
     { 
      sqlDupReader.Close(); 

      //See "How To Use DPAPI (Machine Store) from ASP.NET" for information about securely storing connection strings. 
      String sqlStmt = "Insert into Songs values (@songpath);"; 
      SqlConnection sqlConn = new SqlConnection(@"Data Source = .\SQLEXPRESS; AttachDbFilename = |DataDirectory|\Database.mdf; Integrated Security = True; User Instance = True; uid=sa; pwd=password;"); 
      SqlCommand cmd = new SqlCommand(sqlStmt, sqlConn); 
      SqlParameter sqlParam = null; 

      //Usage of Sql parameters also helps avoid SQL Injection attacks. 
      sqlParam = cmd.Parameters.Add("@userName", SqlDbType.VarChar, 150); 
      sqlParam.Value = Server.MapPath("~/Uploads/") + filename; 

      //Attempt to add the song to the database. 
      try 
      { 
       sqlConn.Open(); 
       cmd.ExecuteNonQuery(); 
       FileUploadControl.SaveAs(Server.MapPath("~/Uploads/") + filename); 
       songList.Items.Add(filename); 
       StatusLabel.Text = "Upload status: File uploaded!"; 
      } 

      catch (Exception ex) 
      { 
       StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
      } 

      finally 
      { 
       sqlConn.Close(); 
      } 
     } 
    } 
} 

但這buttonfunction提供了相同的結果:

protected void uploadClicked(object sender, EventArgs e) 
{ 
    StatusLabel.Text = "FooBar"; 
} 

有沒有人有過這個問題,或者可能知道原因是什麼?

謝謝!

回答

4

我的朋友幫我弄明白了。這是因爲ASP.NET只允許上傳4MB大小的文件。我不得不進入web.config或machine.config文件並將MaxRequestLength的值更改爲大於4096.這解決了它。

+0

不知道爲什麼它只是給我一個網頁不可用的錯誤,而不是告訴我有什麼問題。 – hahuang65 2010-04-05 19:26:17