在客户端使用javascript在Chrome中创建一个文件 [英] create a file using javascript in chrome on client side

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

问题描述

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



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

解决方案

确保你可以使用全新的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是文件名
fileEntry.createWriter(function(fileWriter){
var arr = new Uint8Array(3); //数据长度

arr [0 ] = 97; //字节数据;这些代码是'abc'
arr [1] = 98;
arr [2] = 99;

var blob = new Blob([arr]);

fileWriter.addEventListener(writeend,function(){
//导航到文件,将下载
location.href = fileEntry.toURL ();
},false);

fileWriter.write(blob);
},function(){});
},function(){ });
},function(){});


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.

解决方案

Sure you can, using the brand new APIs: 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() {});

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

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