在客户端的 Chrome 中使用 Javascript 创建文件 [英] Create a file using Javascript in Chrome on client side

查看:57
本文介绍了在客户端的 Chrome 中使用 Javascript 创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

当然可以,使用全新的 API.

Sure you can, using the brand new APIs.

 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() {});

这篇关于在客户端的 Chrome 中使用 Javascript 创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆