Note that there are some explanatory texts on larger screens.

plurals
  1. POfiles do not download in correct format
    primarykey
    data
    text
    <p>Okay, </p> <p>Basically i have been able to upload files to a SQL server database using asp.net and c#. I can view them no problem in the browser but when i download them they seem to lose their original format e.g. word documents lose their docx extension and you have to select to open them as word documents manually.</p> <p>Here is my code so far</p> <pre><code> //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(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload