asp.net FileUpload控件适用于服务器,但不适用于客户端

时间:2010-10-18 13:59:26

标签: asp.net

我对.net fileupload控件发生了一件奇怪的事情。 如果我将远程桌面连接到包含Web应用程序和数据库服务器的服务器,我可以上传文件并将它们存储到varbinary(max)列中。

但是,当客户端从桌面连接到Web服务器时,可以执行所需的所有操作,例如浏览网页,填写存储/保存数据到数据库的表单等。

但是,当他们尝试将pdf上传到服务器时,会发生以下异常: Web应用程序使用.net 3.5,db是sql 2005,代码是c#。并且欢迎见解。代码和例外情况如下。 任何见解都会受到欢迎。

protected void btnSave_Click(object sender, EventArgs e)
{

    int intDocLen = 0;
    string str = "";

    Stream objStream = default(Stream);
    SqlConnection Conn = default(SqlConnection);
    SqlCommand cmdUploadDoc = default(SqlCommand);
    string ConnString = null;
    lblMessage.Text  = "";
    try
    {
        if (FileUpload1.HasFile)
        {
            Guid NewDOCGUID = System.Guid.NewGuid();
            intDocLen = FileUpload1.PostedFile.ContentLength;
            byte[] Docbuffer = new byte[intDocLen];

            objStream = FileUpload1.PostedFile.InputStream;
            objStream.Read(Docbuffer, 0, intDocLen);

            ConnString = ConfigurationManager.ConnectionStrings["CFDConnectionString1"].ConnectionString;
            Conn = new SqlConnection(ConnString);

            if (!isMM)
            {
                string query = "INSERT INTO DisclosureFiles "
                    + "(DisclosuerSummaryId, FileName, Contents, DocGUID, DateModified) "
                    + "VALUES(@DisclosuerSummaryId, @FileName, @Contents, @DocGUID, @DateModified) ";
                cmdUploadDoc = new SqlCommand();
                cmdUploadDoc.CommandType = CommandType.Text;
                cmdUploadDoc.Connection = Conn;
                cmdUploadDoc.CommandText = query;

                cmdUploadDoc.Parameters.Add("@DisclosuerSummaryId",
                    SqlDbType.Int).Value = disclosureId;
                cmdUploadDoc.Parameters.Add("@FileName",
                    SqlDbType.VarChar).Value = FileUpload1.PostedFile.FileName;
                cmdUploadDoc.Parameters.Add("@Contents",
                    SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
                cmdUploadDoc.Parameters.Add("@DocGUID",
                    SqlDbType.UniqueIdentifier).Value = NewDOCGUID;
                cmdUploadDoc.Parameters.Add("@DateModified",
                    SqlDbType.DateTime).Value = DateTime.Now;
            }
            else
            {
                string query = "INSERT INTO DisclosureFiles "
                    + "(massMediaId, FileName, Contents, DocGUID, DateModified) "
                    + "VALUES(@massMediaId, @FileName, @Contents, @DocGUID, @DateModified) ";
                cmdUploadDoc = new SqlCommand();
                cmdUploadDoc.CommandType = CommandType.Text;
                cmdUploadDoc.Connection = Conn;
                cmdUploadDoc.CommandText = query;

                cmdUploadDoc.Parameters.Add("@massMediaId",
                    SqlDbType.Int).Value = disclosureId;
                cmdUploadDoc.Parameters.Add("@FileName",
                    SqlDbType.VarChar).Value = FileUpload1.PostedFile.FileName;
                cmdUploadDoc.Parameters.Add("@Contents",
                    SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
                cmdUploadDoc.Parameters.Add("@DocGUID",
                    SqlDbType.UniqueIdentifier).Value = NewDOCGUID;
                cmdUploadDoc.Parameters.Add("@DateModified",
                    SqlDbType.DateTime).Value = DateTime.Now;

            }
            Conn.Open();
            int result = cmdUploadDoc.ExecuteNonQuery(); //this is where it crashes
            Conn.Close();

            if (result > 0)
            {
                lblMessage.Text = "File Saved Successfully";
            }
        }
        else
        {
            lblMessage.Text = "Please Select a File to Upload";
        }
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Please report the following error: "
            + ex.Message;
    }
    finally
    {
        Conn.Close();
    }

}

异常

System.Data.SqlClient.SqlException:字符串或二进制数据将被截断。该语句已终止。位于System.Data.SqlClient.TdsParter.Run的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)中的System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream) ,System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)中的System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)中的BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result,String methodName,Boolean sendToPipe)at System.Data TestForm.bt中的.SqlClient.SqlCommand.ExecuteNonQuery() nSave_Click(Object sender,EventArgs e)位于c:\ inetpub \ wwwdev \ UploadPdf.aspx.cs:第104行

2 个答案:

答案 0 :(得分:0)

该错误表示您尝试在数据库字段中存储过多数据,例如尝试在varchar(100)字段中存储长度为200的文件名。其他表格列的容量是多少?您要在其中存储的内容的长度是多少?

答案 1 :(得分:0)

尝试逐一对列进行注释并重新执行代码,您似乎已经定义了一个较小的列,并尝试存储导致此错误的数据。

您可以查看定义的列大小,并确保所有列都能够存储文件的数据吗?