2

好的,

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

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

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

3 回答 3

1

尝试将 application/ 添加到您的附件类型。IE

Response.contenttype = "application/" + dr["attachmenttype"].ToString();
于 2013-06-21T11:26:47.633 回答
0

看起来(尽管我不能确定)为文件名保存的值取决于文本框(FilenameTB)中的使用类型,而不是磁盘上文件的名称?

如果您要查看 PostedFile.FileName,我认为您可以提取扩展名?

于 2013-06-21T11:26:12.037 回答
0

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

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

请试试这个。

谢谢

于 2013-06-21T12:54:59.460 回答