PDF download fails in Android Browser

天大地大妈咪最大 提交于 2019-12-29 09:57:08

问题


I have an ASP.Net WebForm that dynamically generates a pdf. The pdf download works in all the desktop browsers, as well as the IPad, Chrome mobile, and Firefox mobile. However, the pdf download fails in the Android browser with the message "Download Unsuccessful" in the notification area. I am writing the pdf to the response with this code:

using (PdfDocument pdf = this.RenderPDF())
using (MemoryStream stream = new MemoryStream())
{
    pdf.Save(stream, false);
    response.Clear();
    response.AddHeader("content-disposition", "inline;filename=myPdf.pdf");
    response.AddHeader("content-length", stream.Length.ToString());
    response.ContentType = "application/pdf";
    response.BinaryWrite(stream.ToArray());
    response.Flush();
    stream.Close();
    response.End();
}

What should I do differently to get the pdf download working in the Android browser?


回答1:


Since you said the PDF is generated by a form, I'm assuming that's in response to a POST. I'm having the same problem, and found this comment on a similar S/O question:

issue is due to a bug in the Android browser and Dolphin, which causes downloads to fail on POST actions. I changed my actions to GET as a workaround.

The issue referenced is Support download of POST responses - open since 2009!


(This question has several iterations on S/O; neither closing nor answering them all seems reasonable, so how about some links:

  • C# Response Shows Download Unsuccessful Error on Android Browser
  • C# Response.Write pdf not working with Android Browser
  • ASP.NET Inconsistent PDF file download results )


来源:https://stackoverflow.com/questions/22637464/pdf-download-fails-in-android-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!