JavaScript blob filename without link

后端 未结 8 1977
一生所求
一生所求 2020-11-22 02:43

How do you set the name of a blob file in JavaScript when force downloading it through window.location?

function newFile(data) {
    var json = J         


        
8条回答
  •  心在旅途
    2020-11-22 03:01

    window.location.assign did not work for me. it downloads fine but downloads without an extension for a CSV file on Windows platform. The following worked for me.

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

提交回复
热议问题