2010-04-05 81 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