Alternative to msSaveOrOpenBlob on Chrome

12,376

As mentioned in another SO response, you need to test for the browser type and use the right API:

var download_csv_using_blob = function (file_name, content) {
    var csvData = new Blob([content], { type: 'text/csv' });
    if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
        window.navigator.msSaveOrOpenBlob(csvData, file_name);
    } else { // for Non-IE (chrome, firefox etc.)
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
        var csvUrl = URL.createObjectURL(csvData);
        a.href =  csvUrl;
        a.download = file_name;
        a.click();
        URL.revokeObjectURL(a.href)
        a.remove();
    }
};

Or also see SO.

Share:
12,376

Related videos on Youtube

Giovanni D'Amico
Author by

Giovanni D'Amico

Updated on June 04, 2022

Comments

  • Giovanni D'Amico
    Giovanni D'Amico almost 2 years

    i need to find alternative to "msSaveOrOpenBlob ", that work on IE, for Firefox and Chrome. On Chrome in particular that not work and don't show the dialog box to chose if save or open file.

    Can you suggest me how can i let open the dialog box to let the user choose if he want to save or open the file?

    I'm actualy using Angular 6.

    • Maidenless Runt
      Maidenless Runt about 2 years
      Is there any update on this problem, have you found an alternative or solution by any chance?
  • Giovanni D'Amico
    Giovanni D'Amico about 4 years
    No man, i need something like this : filestore.community.support.microsoft.com/api/images/…
  • Deepak-MSFT
    Deepak-MSFT about 4 years
    I tried to find but did not get an example to get that popup for save and open a file. I suggest you can provide the link to download the file.
  • Giovanni D'Amico
    Giovanni D'Amico about 4 years
    I just create a popup that let the user chose if download or open the file but i don't know why chrome don't have this stuff.
  • Deepak-MSFT
    Deepak-MSFT about 4 years
    Thanks for sharing the solution to the issue. I suggest you post your solution as an answer for this thread and try to mark your own answer as an answer to this question after 48 hrs when it is available to mark. It can help other community members in the future in similar kinds of issues. Thanks for your understanding