[英]Need help to change file format after the users download it from Database
Did I miss something in this code? 我错过了这段代码吗? cuz when I test it I get the file type as "File" not as PDF.
当我测试它时,我得到的文件类型是“文件”而不是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 https://i.gyazo.com/8eb75d4e55bed17155690531841d80c7.png
Maybe try adding a 也许尝试添加一个
Response.Flush(); Response.Close();
after Response.BinaryWrite(pdfData);
在
Response.BinaryWrite(pdfData);
More importantly it should be while (reader.Read())
rather than if (reader.Read())
更重要的是,它应该是
while (reader.Read())
而不是if (reader.Read())
I'd suggest you read up on here: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read(v=vs.110).aspx 我建议您在这里阅读: 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.