你的位置:首页 > ASP.net教程

[ASP.net教程]下载文档时Safari浏览器下载后出现.html问题


下载代码是需要设置 Response.ContentType = "application/octet-stream",

不要设为application/x-msdownload,改设为applicatoin/octet-stream即可(这种格式代表任意的二进制数据),

ContentType用于定义用户的浏览器或相关设备如何显示将要加载的数据。

 代码如下:

    private static void RenderToBrowser(byte[] bytes, HttpContext context, string downloadname)    {      if (context.Request.Browser.Browser == "IE")      {        downloadname = HttpUtility.UrlEncode(downloadname, System.Text.Encoding.UTF8).Replace("+", "%20");      }      context.Response.ContentType = "application/octet-stream";      context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + downloadname);      context.Response.BinaryWrite(bytes);      context.Response.Flush();      context.Response.End();    }