0

我在数据库中的表中使用了以下结构。我无法在数据库中上传超过 1mb 的文件,请帮忙看看是什么问题。

fileid(int), FileName(varchar(100)), ContentType (varchar(75)), data varbinary(MAX)

protected void btnUpload_Click(object sender, EventArgs e)
{

    // Read the file and convert it to Byte Array

    string filePath = FileUpload1.PostedFile.FileName;

    string filename = Path.GetFileName(filePath);

    string ext = Path.GetExtension(filename);

    string contenttype = String.Empty;



    //Set the contenttype based on File Extension

    switch (ext)
    {

        case ".doc":

            contenttype = "application/vnd.ms-word";

            break;

        case ".docx":

            contenttype = "application/vnd.ms-word";

            break;

        case ".xls":

            contenttype = "application/vnd.ms-excel";

            break;

        case ".xlsx":

            contenttype = "application/vnd.ms-excel";

            break;

        case ".jpg":

            contenttype = "image/jpg";

            break;

        case ".png":

            contenttype = "image/png";

            break;

        case ".gif":

            contenttype = "image/gif";

            break;

        case ".pdf":

            contenttype = "application/pdf";

            break;

    }

    if (contenttype != String.Empty)
    {



        Stream fs = FileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        //insert the file into database

        Transmittallistfortest transmittalList = (Transmittallistfortest)DetailsView1.FindControl("Transmittallistfortest1");
        GridView g3 = transmittalList.FindControl("GridViewTtransmittals") as GridView;
        foreach (GridViewRow di in g3.Rows)
        {

            if (di.RowType == DataControlRowType.DataRow)
            {

                RadioButton rad = di.FindControl("RadioButton1") as RadioButton;
                //Giving Error:Object reference not set to an instance of an object.
                rad.CheckedChanged += new EventHandler(MyCheckedChangeEventHandler);
                if (rad != null && rad.Checked)
                {
                    var w = di.RowIndex;

                    string s = ((HyperLink)di.Cells[1].Controls[0]).Text;

                    var tr = from transmittal in _DataContext.tbltransmittalNos
                             where transmittal.TRANSMITTAL == s
                             select transmittal.TransID;
                    int _transid = tr.SingleOrDefault();
                    // int _transid = Convert.ToInt32(tr.SingleOrDefault()); 
                    Label1.Text = _transid.ToString();
                    Label2.Text = s;
                }
            }
        }

        tblFile fn = new tblFile();

        fn.DocId = _DocID;
        fn.TransId = Convert.ToInt32(Label1.Text);
        fn.FileName = filename;
        fn.ContentType = contenttype;
        fn.Data = bytes;

        // BookAuthor bookAuthor = new BookAuthor();
        _DataContext.tblFiles.InsertOnSubmit(fn);
        _DataContext.SubmitChanges();
        //doctranscon.TransmitToConid = Convert.ToInt32(ddlTransmittaltoCon.SelectedValue);

        lblMessage.ForeColor = System.Drawing.Color.Green;

        lblMessage.Text = "File Uploaded Successfully";


        // Update display
        DisplayDocument();

    }
}
4

2 回答 2

2
  1. 分配数组大小的数据最终会失败。研究流式语义
  2. 检查您配置的maxRequestLength
于 2012-11-13T15:51:48.443 回答
2

您没有分享您的具体错误,但这可能是原因。增加 web.config 文件中的maxRequestLength大小。默认值为 4096 KB (4 MB)。

<configuration>
   <system.web>
      <httpRuntime maxRequestLength="size in kbytes"
      ...             
      />
于 2012-11-13T15:53:21.577 回答