ASP.NET网页不可用

时间:2010-04-05 07:38:26

标签: asp.net webpage

在这里展示ASP.NET的代码非常困难,所以我会尽力描述我的问题。

我有一个FileUploadControl和一个Button,它在单击时调用一个函数。当我的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();
            }
        }
    }
}

但是这个按钮功能提供了相同的结果:

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

以前是否有人遇到此问题,或者可能知道原因是什么?

谢谢!

1 个答案:

答案 0 :(得分:4)

我的朋友帮我搞清楚了。这是因为ASP.NET只允许上传4MB大小。我必须进入web.config或machine.config文件,并将MaxRequestLength的值更改为大于4096.这解决了它。