在客户端使用chrome中的javascript创建文件

Pra*_*een 29 javascript google-chrome javascript-events google-chrome-extension facebook-javascript-sdk

我想知道我是否可以使用javascript创建文本文件并将文件保存在他/她的计算机的用户"下载"部分中.我的功能应该工作的方式是当用户单击提交按钮时,我在文本文件中填充用户信息,然后将其保存在他的机器中.我希望这可以在谷歌Chrome中工作.

这可能吗?我看到的帖子明确告诉我这是一个严重的安全问题.

pim*_*vdb 39

当然可以,使用全新的API:http://jsfiddle.net/4D92b/88/.

 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;

 window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
    fs.root.getFile('test.bin', {create: true}, function(fileEntry) { // test.bin is filename
        fileEntry.createWriter(function(fileWriter) {
            var arr = new Uint8Array(3); // data length

            arr[0] = 97; // byte data; these are codes for 'abc'
            arr[1] = 98;
            arr[2] = 99;

            var blob = new Blob([arr]);

            fileWriter.addEventListener("writeend", function() {
                // navigate to file, will download
                location.href = fileEntry.toURL();
            }, false);

            fileWriter.write(blob);
        }, function() {});
    }, function() {});
}, function() {});
Run Code Online (Sandbox Code Playgroud)

  • @pimvdb链接已损坏 (10认同)

Mag*_*ero 11

在Chrome浏览器中输入此内容

data:text;charset=utf-8,helloWorld
Run Code Online (Sandbox Code Playgroud)

因此,要为您的用户构建下载,您可以执行类似的操作

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

然后将其注入dom,供用户按下.

  • @Flater:使用`download`属性.它适用于Chrome,允许您下载任何文件名的内容. (3认同)

din*_*ygv 6

以下方法适用于 IE11+、Firefox 25+ 和 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>
Run Code Online (Sandbox Code Playgroud)

在行动中看到这个:http : //jsfiddle.net/Kg7eA/

Firefox 和 Chrome 支持数据 URI 导航,这允许我们通过导航到数据 URI 来创建文件,而 IE 出于安全目的不支持它。

另一方面,IE 具有用于保存 blob 的 API,可用于创建和下载文件。


小智 5

尝试这个:

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

如果要设置download锚标记的文件名使用属性:

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

  • 相当惊人......但我如何设置文件名?我得到了“下载”作为文件名,没有扩展名。 (3认同)

归档时间:

查看次数:

54913 次

最近记录:

7 年,10 月 前