Asp.net mvc IE just中的文件下载名称;未命名的.bmp“如何修复?
我正在使用ASP.Net MVC3,并使用以下内容提供文件:Asp.net mvc IE just中的文件下载名称;未命名的.bmp“如何修复?,asp.net-mvc,internet-explorer,Asp.net Mvc,Internet Explorer,我正在使用ASP.Net MVC3,并使用以下内容提供文件: public class ByteResult : ActionResult { public String ContentType { get; set; } public byte[] Bytes { get; set; } public ByteResult(byte[] sourceStream, String contentType) { Bytes = sourceStr
public class ByteResult : ActionResult
{
public String ContentType { get; set; }
public byte[] Bytes { get; set; }
public ByteResult(byte[] sourceStream, String contentType)
{
Bytes = sourceStream;
ContentType = contentType;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.Clear();
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = ContentType;
var stream = new MemoryStream(Bytes);
stream.WriteTo(response.OutputStream);
stream.Dispose();
}
}
对于使用此方法嵌入在页面中的照片,当用户从上下文菜单中选择“将图片/图像另存为…”时,“另存为”对话框将显示所有浏览器的img src属性中的文件名,IE(当然)除外,IE显示“untitled.bmp”。恶心
如何修复IE???向浏览器“建议”文件名的标准方法是包含内容配置。大概是这样的:
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.bmp", someString));
您无需创建自己的ActionResult来返回字节内容。它已经存在了,叫做
在控制器中,您可以使用文件
方法,如下所示:
public ActionResult Do()
{
return File(byteContentOrStream, "image/bmp", "yourfilename.bmp");
}
第三个参数设置内容配置中的文件名
自己设置内容配置会起作用;但这是内置在ASP.NET中的,并根据。例如,如果文件名包含引号,它将处理正确的转义。这通常有效,但不适用于IE。IE是否还缺少其他东西?此外,这还有一个意想不到的后果,即阻止用户在浏览器中自行打开图像,即右键单击“在新选项卡中打开图像”(Chrome)即可直接下载。