[英]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();
}
}
也許嘗試添加一個
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.