Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to download PDF in safari browser on iphone or ipad pro ios12

I have PDF download functionality in my web app. It is working fine with all the browsers and iOS11 but it's not working on safari browser and ios12 on mobile or iod pro. I am getting below error - WebKitBlobResource error 1

export const downloadPDF = (downloadLink, fileName, trackId, productId, historyId) => {
  return (dispatch) => {
    return request.doGetAuth(downloadLink).then(response => {
      let contentType = response.headers.get('content-type');
      if (_.includes(contentType, 'application/json')) {
        return response.json();
      } else {
        return response.blob();
      }
    }).then(blobby => {
      if (!blobby.message) {
        const blob = new Blob([blobby], {
          type: 'application/pdf'
        });
        if (isIos()) {
          if (!isCriOs()) {
            // For ios 
            let url = window.URL.createObjectURL(blob);
            dispatch(downloadReadyAction(url, fileName));
          } else {
            // if chrome
            let reader = new FileReader();
            reader.onload = function(e) {
              dispatch(downloadReadyAction(reader.result, fileName));
            }
            reader.readAsDataURL(blob);
          }
        } else {
          FileSaver.saveAs(blob, fileName);
        }
      }
    }).catch(err => {
      console.log('Problem downloading pdf from server ' + err)
    })
  }
}
like image 721
Kalashir Avatar asked Oct 03 '18 07:10

Kalashir


People also ask

Why is Safari not allowing Downloads?

Clear the Safari Cache An outdated browser cache can wreak havoc and cause all sorts of issues and might result in Safari not downloading files on your Mac. Check if deleting it makes a difference: Click Safari (in the menu bar) and select Preferences. Switch to the Advanced tab.

How do I save a PDF in Safari Pro iPad?

Open Safari and jump to the website you'd like to save. Tap the Share button at the bottom. At the top of the Share Sheet, tap Options > and choose PDF instead of Automatic, then tap Done.

Why can't Safari download a file on my iPad?

If you receive a file via [Open in Browser] after entering a share-link in the Safari mobile web browser, it may be difficult to download the file, because the file that you want to download is a large file or the file has a file format or codec not supported by iOS.

How do I download files from Safari on iPhone or iPad?

To download a file in Safari, just tap on a download link on a website or tap and hold on a link and then tap Download Linked File (Figure B). Downloading a file in Safari on iOS or iPadOS is as easy as tapping and holding on a link and selecting Download Linked File.


1 Answers

When we open pdf in new url tab , The file doesn't exist but its only cache stored inside the browser. So when we generate the blob and redirect to current tab to point to the generated blob url, We lose the cache. So opening url in new window helps it.

   let url = window.URL.createObjectURL(blob);
   window.open(url, "_blank");
like image 195
Kalashir Avatar answered Nov 15 '22 03:11

Kalashir