create a file using javascript in chrome on client

2020-01-23 15:38发布

I would like to know if I can create a text file and save the file in the users "Downloads" section in his/her computer using javascript. The way my feature should work is when the user clicks the submit button, I populate the users info in the text file and then save it in his machine. I would like this to work in google chrome.

Is this possible? I have seen posts that specifically tell me that it is a serious security issue.

9条回答
萌系小妹纸
2楼-- · 2020-01-23 15:53

The following method works in IE11+, Firefox 25+ and Chrome 30+:

<a id="export" class="myButton" download="" href="#">export</a>
<script>
    function createDownloadLink(anchorSelector, str, fileName){
        if(window.navigator.msSaveOrOpenBlob) {
            var fileData = [str];
            blobObject = new Blob(fileData);
            $(anchorSelector).click(function(){
                window.navigator.msSaveOrOpenBlob(blobObject, fileName);
            });
        } else {
            var url = "data:text/plain;charset=utf-8," + encodeURIComponent(str);
            $(anchorSelector).attr("download", fileName);               
            $(anchorSelector).attr("href", url);
        }
    }

    $(function () {
        var str = "hi,file";
        createDownloadLink("#export",str,"file.txt");
    });

</script>

See this in Action: http://jsfiddle.net/Kg7eA/

Firefox and Chrome support data URI for navigation, which allows us to create files by navigating to a data URI, while IE doesn't support it for security purposes.

On the other hand, IE has API for saving a blob, which can be used to create and download files.

查看更多
beautiful°
3楼-- · 2020-01-23 15:55

This link helped me a lot and solved my problem. Cross browser solution:

https://www.thewebflash.com/reading-and-creating-text-files-using-the-html5-file-api/

This is the most relevant part:

if ('msSaveOrOpenBlob' in navigator) {

    navigator.msSaveOrOpenBlob(textFileAsBlob, fileName);
} else {
    var downloadLink = document.createElement('a');
    downloadLink.download = fileName;
    downloadLink.innerHTML = 'Download File';
    if ('webkitURL' in window) {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    } else {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = 'none';
        document.body.appendChild(downloadLink);
    }

    downloadLink.click();
}
查看更多
干净又极端
4楼-- · 2020-01-23 15:57

No, as that would allow you to create malicious programs in the client's computer, and harm his privacy.

Also, requests to download files come from the server, so you'll need to create the file on the server, and serve it to the user, and hope he'll save it (if he requested for it it's likely that he will).

Another possible solution to look at is to use data URIs or CSVs, but browser support for them is incomplete (IE), see Create a file in memory for user to download, not through server

查看更多
家丑人穷心不美
5楼-- · 2020-01-23 16:06

Try this:

document.body.innerHTML+="<a id='test' href='data:text;charset=utf-8,"+encodeURIComponent("hi")+"'>Your Download</a>";
document.getElementById('test').click();

if you want to set the filename use download attribute of anchor tag:

document.body.innerHTML+="<a id='test' href='data:text;charset=utf-8,"+encodeURIComponent("hi")+"' download=yourfilename>Your Download</a>";
document.getElementById('test').click();
查看更多
\"骚年 ilove
6楼-- · 2020-01-23 16:10

Enter this into the Chrome browser

data:text;charset=utf-8,helloWorld

So to construct the download for your users you would do something like

data='<a href='data:text;charset=utf-8,'+uriEncode(yourUSERdataToDownload)+' >Your Download</a>

Then inject it into the dom for your user to press.

查看更多
Viruses.
7楼-- · 2020-01-23 16:12

You will need server side functionality in order to server the user a text file (javascript is not enough). You can create a server side script that creates the file and use javascript in order to prompt user to save it.

查看更多
登录 后发表回答