V
V
Valery Semenenko2021-05-11 18:52:56
safari
Valery Semenenko, 2021-05-11 18:52:56

Problem with Blob encoding in Safari?

There is such an initial version of the code for downloading a text file (text/plain) to the OS (a piece of code is taken out from the angular component\typescript):

...
getTextCompanyInfo(): string {
  let copyText = '';
  
  this.clientInfo.forEach((field: IInfo) => {
    const description = field.description
      ? `${field.description}: `
      : '';
      copyText += `${description}${field.value}\n`;
  });
  return copyText;
}

download(): void {
  StatusTrackerModalComponent.createDownloadLink(
    this.getTextCompanyInfo()
  )
}

static createDownloadLink(
  fileBody: string,
  fileName: string = 'Реквизиты.txt'
  ): void {
    if (window.navigator && window.navigator.msSaveOrOpenBlob) { // если браузер IE11
      window.navigator.msSaveOrOpenBlob(new Blob([fileBody]), fileName);
    } else { // если evergreen браузеры
      const link = window.document.createElement('a');
      link.href = window.URL.createObjectURL(
        new Blob([fileBody], { type: 'text/plain;charset=utf-8' })
      );
      link.download = fileName;
      link.click();
    }
}
...


Then the code was slightly modified to this option (using a plugin for cross-browser compatibility):

import { saveAs } from 'file-saver';
...
getTextCompanyInfo(): string {
  let copyText = '';
  this.clientInfo.forEach((field: IInfo) => {
    const description = field.description
      ? `${field.description}: `
      : '';
    copyText += `${description}${field.value}\n`;
  });
  return copyText;
}

copy(): void {
  const copiedText = this.getTextCompanyInfo();
  copy(copiedText);
}

download(): void {
  const type = 'text/plain;charset=utf-8';
  const copiedText = this.getTextCompanyInfo();
  const blob = new Blob([copiedText], { type });
  saveAs(blob, this.fileName);
}
...


In both cases, the result is the same - in Web or Android (Chrome) - the text file is downloaded in OS - Desktop or Android.
In iOS \ Safari (v.12.4.1) - the file is not downloaded, but opens in such a window ( I can’t even say exactly what kind of window it is, because it’s not an apple tree at all ):
609aa80928f33631871273.jpeg
609aa8179970e081400313.jpeg

Tell me - how to understand what the problem is in general and how to solve it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question