首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ashx强制下载图片

Ashx强制下载图片
EN

Stack Overflow用户
提问于 2011-04-14 12:46:35
回答 2查看 1.4K关注 0票数 1

我有一个显示图像的ashx文件。我不想显示图像,但强制下载。

这是我的代码:

代码语言:javascript
复制
context.Response.AppendHeader("content-disposition", "attachment; filename=" + userId + ".jpg");
context.Response.ContentType = "image/jpeg";
context.Response.OutputStream.Write(msMasterFinal.ToArray(), 0, msMasterFinal.ToArray().Length); 

当我用这个ashx打开我的浏览器时,图像会自动显示。如何强制下载?

非常感谢

EN

回答 2

Stack Overflow用户

发布于 2011-04-14 12:57:20

嗯,你的代码看起来不错。我曾经使用过这个代码片段来实现你想要做的事情:

代码语言:javascript
复制
context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=""{0}""", FileName));
context.Response.AddHeader("Content-Type", FileType);
context.Response.AddHeader("Content-Length", FileSize.ToString);
context.Response.BinaryWrite(FileBytes);

它几乎是相似的..。

票数 1
EN

Stack Overflow用户

发布于 2011-04-15 01:35:03

如果图片是“contentType /jpeg”,你的浏览器总是会在窗口中显示图片。尝试使用不同的contentType。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5658674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档