上传超过1mb是不可能的

时间:2012-11-13 15:44:24

标签: asp.net sql-server

我在数据库中使用了以下结构。我无法在数据库中上传超过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();

    }
}

2 个答案:

答案 0 :(得分:2)

  1. 分配数据大小的数据最终会失败。查看streaming semantics
  2. 检查已配置的maxRequestLength

答案 1 :(得分:2)

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

<configuration>
   <system.web>
      <httpRuntime maxRequestLength="size in kbytes"
      ...             
      />