How to create and download an XML file on the fly using javascript?

后端 未结 5 1428
眼角桃花
眼角桃花 2020-12-29 15:12

I got a requirement as following:

There is a link on a web page. As user clicks on link it should create a file on the fly and a download box pops up. How to do it u

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 15:24

    decodeRequest(textToDecode) {
    
        var decodedString = atob(textToDecode);
    
        var fileName = "fileName1"+'_RQ';
        var fileType = '.xml';
    
        var blob = new Blob([decodedString], { type: fileType });
    
        var a = document.createElement('a');
        a.download = fileName;
        a.href = URL.createObjectURL(blob);
        a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500); 
    
    }
    

提交回复
热议问题