C# Response.Write pdf not working with Android Bro

2019-07-21 13:25发布

问题:

I am currently having huge issues with the android environment with a pdf export. i am using a report viewer control to render a report in to an array of bytes. Next i am using response.binarywrite method to output the byte stream to the browser. This works in every browser as well as iphone and ipad. However, it will not work on android.

The Pdf says that it is corrupted. When i open the pdf in notepad i see that it is exporting my entire page html instead of the byte array generated by the report viewer.

the code:

    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    string filename = "attachment; filename=Data.pdf";

    byte[] bytes = ReportViewer1.ServerReport.Render(
       "PDF", null, out mimeType, out encoding,
        out extension,
       out streamids, out warnings);

    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", filename);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();

回答1:

You need to remove all HTML from your page (design time).



回答2:

According to the open Android issue 11422 the handling of content-disposition doesn't conform to RFCs. Try quoting the filename value (filename="Data.pdf") and see if it saves correctly.

Also, this StackOverflow answer suggests setting the MIME-type in addition to the content type.



回答3:

If there's any HTML before this code, then that will get sent to the browser because Response.Clear() only clears buffered HTML output, and Response.Buffer only works if no HTML has been outputted.

Try either preventing any output before the code (somehow) or moving this code to its own file. Then get rid of the output buffering code.

PS: This may or may not work; I haven't had a chance to test it. Also there's a lot of issues with Android's content-disposition, so you may want to set the MIME type as GalacticCowboy suggests.