kpg
kpg

Reputation: 671

Desktop web site can't open pdf file on mobile devices - how to

VS2010 VB.NET ASP.NET

I have a simple site where the user clicks a button and a pdf file is displayed in a new broswer window.

To do the this a button launches a new browser window with the following code in the page load event:

            Response.Clear()
            Response.ClearContent()
            Response.ClearHeaders()

            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
            Response.Buffer = True

            Response.ContentType = "application/pdf"
            'Response.ContentType = "text/plain"

            If Download_Flag Then
                'force SaveAs  (this is the download option)
                Response.AppendHeader("Content-Disposition", "attachment; filename=" & "myfile" & ".pdf")
            Else
                'open in borwser
                Response.AddHeader("Content-Disposition", "inline")
            End If

            Response.OutputStream.Write(PdfBuffer, 0, PdfBuffer.Length)
            Response.OutputStream.Flush()
            Response.OutputStream.Close()
            Response.End()

PDFBuffer is a byte array containing the pdf document

This code displays the pdf file in the browser (ie, chrome, mozilla, safari?) using whatever pdf plugin is installed.

if download_flag is true then the pdf is not opened in the viewer but instead browser prompts with the save file dialog

As I said, this code works in desktop borwsers, but it does not work on an ipad or an android phone, and propably other devices as well.

The site is not mobile aware, but if need be it could, I suppose.

I know that apple does not allow downloads, and android may not be able to open open an additional window, so how can I open a pdf file on a button click on these devices? I know he ipad and the android can both open pdf files, I've done it on other sites.

Upvotes: 1

Views: 6222

Answers (2)

kpg
kpg

Reputation: 671

My solution was to open the pdf file in the same window as the site. On android this causes a file download to which the user can go to the downloads folder and view the pdf. On ipad this opens the default pdf viewer. If ipad user download the adobe pdf viewer they can switch over to the adobe reader from the default ipad reader; the adobe reader allows print and email of document. On desktop browser this solution shows the pdf in the same window and user can use the browser's back button to get back to the site when they are done with the pdf. A more robust solution would be to detect the client and act appropriately, but with this common implementation is workable.

Upvotes: 3

Related Questions