文件下载格式不正确 [英] files do not download in correct format

查看:94
本文介绍了文件下载格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,

基本上,我已经能够使用asp.net和c#将文件上传到SQL Server数据库.我可以在浏览器中查看它们没有问题,但是当我下载它们时,它们似乎失去了原始格式,例如Word文档失去了docx扩展名,您必须选择手动将它们作为Word文档打开.

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.

到目前为止,这是我的代码

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();
                }
            }
        }
    }

推荐答案

您需要将扩展​​名/附件类型添加到Response.AddHeader代码中,例如:

You need to add the extension/attachment type to the Response.AddHeader code like:

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

请尝试这个.

谢谢

这篇关于文件下载格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆