html或java脚本代码在硬盘中创建文本文件 [英] html or java script code to create a text file in hard disk

查看:157
本文介绍了html或java脚本代码在硬盘中创建文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



结果应该是一个html文件,
当双击html文件时,它需要在硬盘(本地)的给定路径中创建文本文件。



谢谢。

解决方案

有些东西告诉我,您没有足够的经验知道网页不能在位置的用户空间中创建文件无论如何,的一种方式,但您只能在沙箱中创建文件, 即一个由浏览器分配的保留和保留的空间。



只有Chrome等最新的浏览器才有这种功能......现在就没有其他功能了。



首先,您必须要求一些配额:

  storageInfo。 requestQuota(PERSISTENT,bytes,function(quota){
requestFileSystem(PERSISTENT,quota,gotQuota,errorHandler);
},函数(e){
alert(无法请求配额:+ e);
});

您必须定义两个回调函数:一个用于成功( gotQuota ),一个失败( errorHandler )。

 函数gotQuota(fs){
//创建一个文件
fs.root.getFile('file.txt',{create:true},function(fileEntry){
fileEntry .createWriter(function(fw){
fileWriter.onwriteend = function(e){
console.log(Write successful。);
};
fileWriter.onerror =函数(e){
console.log(Write failed:+ e);
};
fw.write(new Blob([This is the content],{type :text / plain});
});
},errorHandler);
}

Man,它很复杂...请记住,其中一些函数是供应商的前缀(例如 webkitStorageInfo )。



参考资料


Please someone give me a code to create a text file in hard drive

Result should be a html file, when double click html file it need to create a text file in a given path in hard drive(local).

Thank you.

解决方案

Something tells me that you haven't enough experience to know that a web page cannot create a file in the user's space in the position you want.

Anyway, there is a way, but you can only create a file in a sandbox, i.e. a reserved and protected space assigned by the browser.

This is possible only in the most recent browsers like Chrome... and nothing else, for now.

First, you'll have to ask for some quota:

storageInfo.requestQuota(PERSISTENT, bytes, function(quota) {
   requestFileSystem(PERSISTENT, quota, gotQuota, errorHandler);
}, function(e) {
   alert("Couldn't request quota:" + e);
});

You'll have to define two callback functions: one for success (gotQuota) and one for failure (errorHandler).

function gotQuota(fs) {
    // Creates a file
    fs.root.getFile('file.txt', {create: true}, function(fileEntry) {
       fileEntry.createWriter(function(fw) {
           fileWriter.onwriteend = function(e) {
               console.log("Write successful.");
           };
           fileWriter.onerror = function(e) {
               console.log("Write failed: " + e);
           };
           fw.write(new Blob(["This is the content"], {type: "text/plain"});
       });
    }, errorHandler);
}

Man, it's complicated... Keep in mind that some of these function are vendor prefixed (e.g. webkitStorageInfo).

Reference.

这篇关于html或java脚本代码在硬盘中创建文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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