Response.AddHeader("Content-Disposition") 未在 IE6 中打开文件 [英] Response.AddHeader("Content-Disposition") not opening file in IE6

查看:19
本文介绍了Response.AddHeader("Content-Disposition") 未在 IE6 中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.HtmlEncode(FileName));为用户弹出打开/保存文件"对话框,以便他们可以将文件下载到本地计算机上.

I'm using Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.HtmlEncode(FileName)); to pop a 'open/save file' dialog for the users, so that they can download an file on to their local machines.

这在 IE7 中正常工作,但在 IE6 上,当用户单击打开/保存文件"对话框中的打开按钮时,文件未打开.我浏览了网络,发现Response.AddHeader("Content-Disposition", "inline; filename="+Server.HtmlEncode(FileName));应该提供在 IE6 中工作,并且它的工作正常..

This is working good normally in IE7,But on IE6 the file is not opening when user click on the open button in 'open/save file' dialog. I gone through the net and found that Response.AddHeader("Content-Disposition", "inline; filename="+Server.HtmlEncode(FileName)); should be provide to work that in IE6,and its works fine..

但问题是大多数可以在浏览器中打开的文件在页面本身上打开..即用户在邮件页面上并单击下载它在那里打开的图像文件,我需要它在另一个窗口中打开,如IE7 的情况我能做什么...其他无法在 bowser 中打开的文件在系统 ie(word、excel 等)中使用当前应用程序打开.

But the issue is most of the files that can open in browser opens on the page itself.. ie user on a mail page and click download an image file it opens there,, i need it to open in another window as in case of IE7 what can i do... other files that cannot open in bowser open with current application in system ie(word,excel etc)..

请提出一种方法来为两个 IE 坚持使用相同的代码......我使用的代码在这里......

please suggest a method to stick with same code for both IEs... The Code i used is here....

Response.AddHeader("Content-Disposition", "attachment; filename=" +FileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
Response.End();

 private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".xls":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            default:
                return "application/octet-stream";
        }
    }

推荐答案

我在 IE 6 中发现了这个问题,我们必须在 IE 6 中读取内容并使用缓冲区和二进制写入来打开文件,下面的代码工作正常为我在 IE6

i have found the problem in IE 6 we have to read the content and use buffers and binary write to open file in IE 6,, the code below works fine for me in IE6

FileStream sourceFile = new FileStream(Server.MapPath(@"FileName"), FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();

这篇关于Response.AddHeader("Content-Disposition") 未在 IE6 中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆