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

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

    After try what Andreas said I will add something:

    Script:

    function createAndOpenFile(){
        var stupidExample = 'something';
        document.open('data:Application/octet-stream,' + encodeURIComponent(stupidExample));
    }
    

    You have a link like this, note the new download atribute, with it you put the file name.

    Donwload
    

    It works at least in Chrome 27 and Firefox 21.

    Improved are welcome :-)

提交回复
热议问题