C# 用户从数据库下载文件后,需要帮助更改文件格式吗
我在代码中遗漏了什么吗?因为当我测试它时,我得到的文件类型是“file”而不是PDFC# 用户从数据库下载文件后,需要帮助更改文件格式吗,c#,C#,我在代码中遗漏了什么吗?因为当我测试它时,我得到的文件类型是“file”而不是PDF protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString); conn.Open();
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())
我建议您阅读以下内容:您需要将内容类型
标题设置为application/pdf
您有SQL注入漏洞。仍然是相同的文件类型:(我做了,但仍然没有更改:(MIME类型区分大小写。应该是小写的。类型文件没有更改:(