Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net download file error when choosing 'Open' in IE9 [closed]

I am using Response to have my application open up a Word Document for the user. If the user chooses to save the file, it saves it and the file looks how it should when you open it. If the user chooses to open the file right away, they receive an error saying that IE couldn't open the file. If they choose 'Retry', MS Word shows an error saying it cannot find the file. Below are screen shots showing my situation. Also, here is the code I have to display the file:

        this.Context.Response.Clear();
        this.Context.Response.ClearContent();
        this.Context.Response.ClearHeaders();
        this.Context.Response.BufferOutput = true;
        this.Context.Response.ContentType = "application/msword";
        this.Context.Response.AppendHeader("Content-Length", bytes.Length.ToString()); 
        this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
        this.Context.Response.BinaryWrite(bytes);
        this.Context.ApplicationInstance.CompleteRequest();

Here is the screen when prompting the user of the download: enter image description here

Here is the screen after user chooses 'Open' enter image description here

Here is the screen after user chooses 'Retry'. This screen is coming for MS Word. enter image description here

****EDIT**** I have found a bit of code online that I tried testing and issue still occures when I call this function:

    protected void GenerateMsWordDoc()
    {
        string strBody = "<html>" +
            "<body>" + 
                "<div>Your name is: <b>Billy Bob</b></div>" +
                "<table width='100%' style='background-color:#cfcfcf;'><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
                "Ms Word document generated successfully." +
            "</body>" +
            "</html>";
        string fileName = "MsWordSample.doc";
        // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
        Response.Write(strBody);
    }
like image 606
Eric R. Avatar asked Mar 27 '12 17:03

Eric R.


2 Answers

Spaces in the filename parameter of content-disposition have been known to cause errors across different browser versions. Try enclosing the filename in double quotes:

this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + "Test Document.doc" + "\"");
like image 51
Mike Avatar answered Nov 14 '22 19:11

Mike


Can you post the sample data being used? I tried below code in IE9 works fine.

this.Context.Response.Clear();
this.Context.Response.ClearContent();
this.Context.Response.ClearHeaders();
this.Context.Response.BufferOutput = true;
this.Context.Response.ContentType = "application/msword";
this.Context.Response.AppendHeader("Content-Length", "12");
this.Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test Document.doc");
this.Context.Response.BinaryWrite(new byte[] { });
this.Context.ApplicationInstance.CompleteRequest();

Your recent code is also working fine. I am using IE9. Below are version details...

enter image description here

like image 1
Pankaj Avatar answered Nov 14 '22 19:11

Pankaj