I am trying to download a PDF that is in a blob format on iOS. It works as expected on Desktop/Android, but does not work as expected on any browser on iOS. Does anyone have any insight as to why this does not work?

export function setBlobtoLink (url, fileName) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'blob';
  xhr.setRequestHeader('Content-Type', 'application/pdf');
  xhr.onload = function xhrOnload () {
    if (this.status === 200) {
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(this.response);
      } else {
        const data = window.URL.createObjectURL(this.response);
        const link = document.createElement('a');
        document.body.appendChild(link);
        link.style.display = 'none';
        link.href = data;
        link.download = fileName;
        link.target = '_blank';
        link.click();
        window.URL.revokeObjectURL(data);
        link.remove();
      }
    }
  };
  xhr.send();
}

0 Answers