2

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();
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
mscard02
  • 21
  • 1
  • 3
  • @Yuliy - because it works on every platform except Android. – GalacticCowboy Jul 06 '11 at 17:37
  • This does not work for me in chrome either, I think it has something to do with the built in viewer. – CrazyDart Jul 06 '11 at 18:00
  • My page is working fine for me with chrome, its only android. It looks like the issue is it is exporting my page html instead of the byte array the report viewer returns to me, i am not sure why. – mscard02 Jul 06 '11 at 20:18
  • Is this code within a page that contains html? Or is the code called from a page with html? – grant Jul 06 '11 at 22:23
  • the code was called from a page with html because the user has dropdowns and checkboxes they can alter for the data they want to generate. – mscard02 Jul 07 '11 at 12:06
  • This problem sounds like it might have the same cause as a few other open questions on S/O. See my answer here: http://stackoverflow.com/a/25418934/665376 – Kimberly Aug 21 '14 at 05:42
  • any solution about it? My issue: _IPhone works, some Android works, buy anothers not_. – Kiquenet Mar 01 '16 at 14:47

3 Answers3

0

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

Mrchief
  • 75,126
  • 20
  • 142
  • 189
0

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.

Community
  • 1
  • 1
GalacticCowboy
  • 11,663
  • 2
  • 41
  • 66
  • thanks for the help. I tried all of the suggestions to no avail. It still looks like its exporting the pages html instead of the byte array i created from the report viewer, even though i am specifically exporting the byte array from the viewer – mscard02 Jul 06 '11 at 20:17
0

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.

grant
  • 735
  • 8
  • 17
  • I have generated the byte array on the first page, stored it in session, redirected to a new page with no html that grabs the array out of session and response.writes to the user. Now my pdf is empty. I know how to set the content type as application/pdf but how do you set the mime type? – mscard02 Jul 07 '11 at 12:07
  • @mscard02 - If the pdf is empty then either the code on the first page is failing (maybe setting the session to nothing) or the session is disappearing for some reason. Can you set a breakpoint in your code and find out where the byte array is going? Also, I think the mime type is the same thing as content type, so I'm not sure what he's talking about... – grant Jul 07 '11 at 22:56
  • I dropped a breakpoint on the byte array before it is about to be sent through response and it is almost 8mb but when i click save the pdf on the android the file is about 120kb. – mscard02 Jul 08 '11 at 19:36