我无法在点击按钮上下载图像..我已将按钮放在网格内以下载相应的图像..下面是我的代码。
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["FileContent"];
contentType = sdr["Extentions"].ToString();
fileName = sdr["FileName"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AddHeader("Content-type", contentType);
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
答案 0 :(得分:1)
将CommandText
更改为此:
cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=@Id";
读者读到这个:
while(sdr.Read())
{
bytes = (byte[])sdr["FileContent"];
contentType = sdr["Extentions"].ToString();
fileName = sdr["FileName"].ToString();
}
编辑:为了测试,我使用以下代码从我的磁盘而不是DataBase获取文件:
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
// Test code to download 123.sql file from C:\
bytes = System.IO.File.ReadAllBytes("C:\\123.sql");
contentType = ".sql";
fileName = "123.sql";
//string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
//using (SqlConnection con = new SqlConnection(constr))
//{
// using (SqlCommand cmd = new SqlCommand())
// {
// cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id;
// cmd.Parameters.AddWithValue("@Id", id);
// cmd.Connection = con;
// con.Open();
// using (SqlDataReader sdr = cmd.ExecuteReader())
// {
// sdr.Read();
// bytes = (byte[])sdr["FileContent"];
// contentType = sdr["Extentions"].ToString();
// fileName = sdr["FileName"].ToString();
// }
// con.Close();
// }
//}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AddHeader("Content-type", contentType);
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
编辑2: Here是我使用的测试项目的链接。