JavaScript blob filename without link

后端 未结 8 1925
一生所求
一生所求 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:07

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

提交回复
热议问题