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

后端 未结 5 1415
眼角桃花
眼角桃花 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:43

    You can use blobs as shown in this example http://html5-demos.appspot.com/static/a.download.html

    You can have a javacript function with the following code

    var xmltext = "";
    var pom = document.createElement('a');
    
    var filename = "file.xml";
    var pom = document.createElement('a');
    var bb = new Blob([xmltext], {type: 'text/plain'});
    
    pom.setAttribute('href', window.URL.createObjectURL(bb));
    pom.setAttribute('download', filename);
    
    pom.dataset.downloadurl = ['text/plain', pom.download, pom.href].join(':');
    pom.draggable = true; 
    pom.classList.add('dragout');
    
    pom.click();
    

提交回复
热议问题