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/ を添付ファイルの種類に追加してみてください。いえ

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

ファイル名に保存された値は、ディスク上のファイルの名前ではなく、使用がテキストボックス(FilenameTB)に入力したものに依存しているように見えますが(確かなことはわかりませんが)?

PostedFile.FileName を見ると、拡張子を抽出できると思いますか?

于 2013-06-21T11:26:12.037 に答える