2012-11-13 44 views
0

我已經在我的表中使用了以下結構在數據庫中。我無法在數據庫中上傳超過1MB的文件,請幫助解決問題。上傳超過1MB是不可能的

FILEID(INT),文件名(VARCHAR(100)),的ContentType(VARCHAR(75)),數據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(); 

    } 
} 
+0

你確定它是數據庫而不是IIS嗎? – fnurglewitz

回答

2

您沒有分享你的特定錯誤,但是這很可能是原因。在web.config文件中增加maxRequestLength大小。默認值爲4096 KB(4 MB)。

<configuration> 
    <system.web> 
     <httpRuntime maxRequestLength="size in kbytes" 
     ...    
     /> 
相關問題