7
votes

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.

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..

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)..

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";
        }
    }
2
Why are you HTML encoding the filename? HTTP headers are not HTML.spender
yh itz a mistake i have removed it but it doesnt solve the problem..deepu
i have got my solution from part of your question thanks :)bhargav

2 Answers

15
votes

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();
2
votes

Try this setting the content type to octet stream:

Response.ContentType = "application/octet-stream";