【发布时间】:2019-08-23 06:47:44
【问题描述】:
protected void downloadbtn(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, Type from demons where ID=@Id";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["Type"].ToString();
fileName = sdr["Name"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
//文件类型不随文件一起发送 // 例如,如果要下载一张照片,则将下载一个普通文件,然后您需要专门使用绘画或照片应用程序打开它
【问题讨论】:
-
“类型丢失”是什么意思?
contentType变量的内容是什么? -
它询问“另存为类型=所有文件”而不是 .JPEG 或 .MP4 或 .txt
-
好的,这是第一个问题,第二个问题?
-
只有一个问题。
-
我问了 2 个问题,如果您希望人们帮助您,请花点时间阅读。