簡體   English   中英

用戶從數據庫下載文件后需要幫助來更改文件格式

[英]Need help to change file format after the users download it from Database

我錯過了這段代碼嗎? 當我測試它時,我得到的文件類型是“文件”而不是PDF。

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString); 
    conn.Open();
    string pdffile = "select pdf from users where ID='" + TextBoxLic.Text + "'";
    SqlCommand pdfcom = new SqlCommand(pdffile, conn);
    SqlDataReader reader = pdfcom.ExecuteReader();
    if (reader.Read())
    {
        Byte[] pdfData = (byte[])reader.GetValue(0);
        Response.ContentType = "Application/pdf";            
        Response.AppendHeader("content-disposition", "attachment; filename=" + TextBoxLic.Text);
        Response.BinaryWrite(pdfData);
        Response.End();
        conn.Close();
    }
}

https://i.gyazo.com/8eb75d4e55bed17155690531841d80c7.png

也許嘗試添加一個

Response.Flush(); Response.Close();

Response.BinaryWrite(pdfData);

更重要的是,它應該是while (reader.Read())而不是if (reader.Read())

我建議您在這里閱讀: https : //msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.read(v=vs.110).aspx

您需要將Content-Type標頭設置為application/pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM