Chrome上msSaveOrOpenBlob的替代方法 [英] Alternative to msSaveOrOpenBlob on Chrome

查看:397
本文介绍了Chrome上msSaveOrOpenBlob的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到" msSaveOrOpenBlob的替代项",该功能可在IE和Firefox和Chrome上运行.特别是在Chrome上,该按钮无法正常工作,并且不会显示对话框来选择保存还是打开文件.

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?

我实际上是在使用Angular 6.

I'm actualy using Angular 6.

推荐答案

如另一个 SO响应所述,您需要测试浏览器类型并使用正确的API:

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();
    }
};

或者也请参见这样.

这篇关于Chrome上msSaveOrOpenBlob的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆