Files do not load in the correct format

Good,

Basically, I could upload files to the SQL server database using asp.net and C #. I don’t see any problems in the browser, but when I download them, they seem to lose their original format, for example. text documents lose the docx extension, and you must choose to open them as text documents manually.

Here is my code so far

//Method used to upload a file to the database protected void UploadBut_Click(object sender, EventArgs e) { Stream inpStream = DocumentsUploadControl.PostedFile.InputStream; BinaryReader br = new BinaryReader(inpStream); Byte[] size = br.ReadBytes ((int)inpStream.Length); using (SqlConnection conn = new SqlConnection("Data Source=conn\\sqlexpress;Initial Catalog=new catalog;Integrated Security=True")) { string sql = "INSERT INTO Attachments(AttachmentReferenceID, AttachmentType, Filename, AttachmentDescription, FileUploadedBy, UploadDate)" + "values (@Reference, @Type, @Filename, @Descr, @UploadedBy, @UploadedDate)"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@Reference", ReferenceDDL.Text.Trim()); cmd.Parameters.AddWithValue("@Type", DocumentsUploadControl.PostedFile.ContentType.ToString()); cmd.Parameters.AddWithValue("@Filename", FilenameTB.Text.Trim()); cmd.Parameters.AddWithValue("@Descr", size); cmd.Parameters.AddWithValue("@UploadedBy", Session["username"].ToString()); cmd.Parameters.AddWithValue("@UploadedDate", DateTime.Now.Date.ToShortDateString()); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); Response.Redirect("Documents.aspx"); } } } //listener used to download the file protected void lnkDownload_Click(object sender, EventArgs e) { LinkButton lnkbtn = sender as LinkButton; GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; int fileId = Convert.ToInt32(DocumentsGridView.DataKeys[gvrow.RowIndex].Value.ToString()); using (SqlConnection conn = new SqlConnection("Data Source=conn\\sqlexpress;Initial Catalog=new catalog;Integrated Security=True")) { string sql = "SELECT AttachmentReferenceID, AttachmentType, Filename, AttachmentDescription, FileUploadedBy, UploadDate FROM Attachments WHERE AttachmentID=@ID "; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@ID", fileId); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Response.ContentType = dr["AttachmentType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"] + "\""); Response.BinaryWrite((byte[])dr["AttachmentDescription"]); Response.End(); conn.Close(); } } } } 
+2
source share
3 answers

You need to add the extension / attachment type to the Response.AddHeader code, for example:

 Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"] + "." + dr["AttachmentType"].ToString()); 

Please try this.

thanks

0
source

Try adding an app / to your attachment. I.e

 Response.contenttype = "application/" + dr["attachmenttype"].ToString(); 
+1
source

It seems that (although I can’t say for sure) the value stored for the file name depends on the type of use in the text field (FilenameTB), and not on the name of the file on disk?

If you looked at FileFileName, I think you could extract the extension?

0
source

Source: https://habr.com/ru/post/1273191/


All Articles