ASHX 图像下载另存为 ASHX
当用户单击我的链接时,它会弹出“另存为”对话框,但它希望在 IE 以外的任何其他浏览器中将其另存为 ashx 文件。该文件是从数据库中获取的标准 jpeg。什么会导致这种情况发生?
string id = ctx.Request.QueryString["id"];
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand("EBig", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EID", id);
byte[] pict = null;
ctx.Response.ContentType = "image/pjpeg";
try
{
con.Open();
pict = (byte[])cmd.ExecuteScalar();
ctx.Response.Clear();
ctx.Response.AppendHeader("content-disposition", "attachment;");
ctx.Response.ContentType = "image/pjpeg";
ctx.Response.BinaryWrite(pict);
ctx.Response.Flush();
ctx.Response.End();
}
catch
{
}
finally
{
con.Close();
}
When a user clicks my link it brings up the save as dialog, but it wants to save it as a ashx file in any other browser than IE. The file is standard jpeg that is being taken from a database. What would cause this to do?
string id = ctx.Request.QueryString["id"];
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand("EBig", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EID", id);
byte[] pict = null;
ctx.Response.ContentType = "image/pjpeg";
try
{
con.Open();
pict = (byte[])cmd.ExecuteScalar();
ctx.Response.Clear();
ctx.Response.AppendHeader("content-disposition", "attachment;");
ctx.Response.ContentType = "image/pjpeg";
ctx.Response.BinaryWrite(pict);
ctx.Response.Flush();
ctx.Response.End();
}
catch
{
}
finally
{
con.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hanselman 对此有一篇文章。它涉及在
Content-Disposition
响应标头上设置名为filename
的附加属性。相关的RFC在这里,相关部分是2.3:
Hanselman has a post on this. It involves setting an additional attribute named
filename
on yourContent-Disposition
response header.The related RFC is here, relevant section is 2.3:
您需要添加文件名:
You need to add the filename: