如何设置一个blob文件在JavaScript的名称时,强制下载它通过window.location?

function newFile(data) {
    var json = JSON.stringify(data);
    var blob = new Blob([json], {type: "octet/stream"});
    var url  = window.URL.createObjectURL(blob);
    window.location.assign(url);
}

运行上面的代码立即下载一个文件,而不需要页面刷新,如下所示:

bfefe410-8d9c-4883-86c5-d76c50a24a1d

我想把文件名设为my-download。json。


当前回答

晚了,但因为我遇到了同样的问题,我添加了我的解决方案:

function newFile(data, fileName) {
    var json = JSON.stringify(data);
    //IE11 support
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        let blob = new Blob([json], {type: "application/json"});
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {// other browsers
        let file = new File([json], fileName, {type: "application/json"});
        let exportUrl = URL.createObjectURL(file);
        window.location.assign(exportUrl);
        URL.revokeObjectURL(exportUrl);
    }
}

其他回答

Window.location.assign不适合我。它可以下载,但在Windows平台上下载CSV文件时没有扩展名。下面的方法对我很有效。

    var blob = new Blob([csvString], { type: 'text/csv' });
    //window.location.assign(window.URL.createObjectURL(blob));
    var link = window.document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    // Construct filename dynamically and set to link.download
    link.download = link.href.split('/').pop() + '.' + extension; 
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
saveFileOnUserDevice = function(file){ // content: blob, name: string
        if(navigator.msSaveBlob){ // For ie and Edge
            return navigator.msSaveBlob(file.content, file.name);
        }
        else{
            let link = document.createElement('a');
            link.href = window.URL.createObjectURL(file.content);
            link.download = file.name;
            document.body.appendChild(link);
            link.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));
            link.remove();
            window.URL.revokeObjectURL(link.href);
        }
    }

晚了,但因为我遇到了同样的问题,我添加了我的解决方案:

function newFile(data, fileName) {
    var json = JSON.stringify(data);
    //IE11 support
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        let blob = new Blob([json], {type: "application/json"});
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {// other browsers
        let file = new File([json], fileName, {type: "application/json"});
        let exportUrl = URL.createObjectURL(file);
        window.location.assign(exportUrl);
        URL.revokeObjectURL(exportUrl);
    }
}

这是一个很好的简单的解决方法。

function downloadBloob(blob,FileName) {
    var link = document.createElement("a"); // Or maybe get it from the current document
    link.href = blob;
    link.download = FileName;
    link.click();
}

这就是我的解。在我看来,你不能绕过<a>。

函数export2json() { Const data = { 答:“111”, 乙:“222”, c:“333” }; const a = document.createElement("a"); a.href = URL.createObjectURL( 新团([JSON。Stringify (data, null, 2)], { 类型:“application / json” }) ); a.setAttribute(“下载”,“data.json”); document.body.appendChild(一个); a.click (); document.body.removeChild(一个); } <按钮onclick = " export2json ()>导出数据到json文件</button>