onais ahmer

onais ahmer

  • NA
  • 307
  • 4.5k

How to store large txt file and covert into binary

Aug 9 2018 5:17 AM
i am trying to uploading the txt file near 1 GB
 
i modify the Web.config file but
 
<httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
  1. protected void Upload(object sender, EventArgs e)  
  2. {  
  3.     string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);  
  4.     string contentType = FileUpload1.PostedFile.ContentType;  
  5.     using (Stream fs = FileUpload1.PostedFile.InputStream)  
  6.     {  
  7.         using (BinaryReader br = new BinaryReader(fs))  
  8.         {
  9.             br.ReadBytes((Int32)fs.Length) );
  10.             // At this line i got the errorr msg  
  11.             byte[] bytes = br.ReadBytes((Int32)fs.Length);
  12.              string constr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
  13.             using (SqlConnection con = new SqlConnection(constr))  
  14.             {  
  15.                 string query = "insert into Files values (@Name, @ContentType, @Data)";  
  16.                 using (SqlCommand cmd = new SqlCommand(query))  
  17.                 {
  18.                     cmd.Connection = con;  
  19.                     cmd.Parameters.AddWithValue("@Name", filename);  
  20.                     cmd.Parameters.AddWithValue("@ContentType", contentType);  
  21.                     cmd.Parameters.AddWithValue("@Data", bytes);  
  22.                     con.Open();  
  23.                     cmd.ExecuteNonQuery();  
  24.                     con.Close();  
  25.                 }  
  26.             }  
  27.         }  
  28.     }  
  29.     Response.Redirect(Request.Url.AbsoluteUri);  
  30. }

Answers (8)