本文介绍了JavaScript / Ajax写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我明白这个问题很多,但这里的区别是我有WEB安全禁用。
我正在创建一个永远不会被托管在任何地方的web应用程序,以访问用户运行 chrome --disable-web-security --user-data-dir [webappdirectoryhere]
我明白这是多么异常,但它只是一个小项目。



我使用这个ajax来访问本地文件(这不会在Chrome中工作,除非你有一个服务器运行,或者你只是像我一样禁用Web安全性):
$ b $ pre $ function loadDoc(){
$ .ajax({url:ajax_info.txt,success:function(result){
var variablesArray = result.split();
}});





$ b

所以我的问题是这样的 - 我可以做这样的事情,但为了写作阅读?



编辑:
所以我真正想要的是一个函数,做那个ajax的逆。并写下类似于(变量1 ++变量2)
的注释:我想覆盖,而不是追加。

p>既然你说过你不打算在任何地方托管它,你可以将你的代码移动到一个Packaged应用程序中。从那里你可以使用 api。



使用向用户询问他们希望应用能够读取/写入哪个目录。在回调中检查返回的条目是否有效,然后将其存储起来以备后用。

  var userDir = null; 
chrome.fileSystem.chooseEntry({type:'openDirectory'},function(theEntry){
//执行完整性检查并存储它
if(!theEntry.isDirectory){
//报告错误
返回;
}
userDir = theEntry;
});

一旦你有一个目录项引用,你可以使用获取对文件的引用,如果文件尚不存在,创建子目录也是如此,只需用 getDirectory 替换 getFile 即可。然后使用获取FileWriter实例写入该文件。

 函数saveData(文件名,数据){
if(!userDir){
//报告错误
return;

$ b $ userDir.getFile(filename,{create:true},function(fileEntry){
if(!fileEntry ||!fileEntry.isFile){
//报告错误
return;
}

fileEntry.createWriter(function(fileWriter){
fileWriter.onwriteend = function(e){
/ /报告成功
};
fileWriter.onerror =函数(e){
//报告错误:e.toString()
};

//从数据创建一个Blob并写入它
var blob = new Blob([data],{type:'text / plain'});
fileWriter.write(blob);
});
});
}

//在用户选择目录
saveData(log.txt,某些数据)后的某个时间点;

查看各种文件以获得错误报告和其他必需品。

如果您只想向用户询问目录,请使用保存目录条目的ID。并使用获取目录的引用。从那里只需要执行saveData函数中的步骤。检查其他进程的文档,如阅读文件。


I understand this question is asked a lot but the distinction here is I HAVE WEB SECURITY DISABLED.I am creating a webapp that will never be hosted anywhere, to access the user runs chrome --disable-web-security --user-data-dir [webappdirectoryhere]I understand how unorthodox this is but it's just for a small project.

I am using this ajax to access a local file (this wouldn't work in chrome unless you have a server running or you just disable web security like I have):

function loadDoc(){
    $.ajax({url: "ajax_info.txt", success: function(result){
        var variablesArray = result.split(" ");
}});
}

So my question is this - can I do something like this but for writing instead of reading?

EDIT:So what I really want is a function that does the inverse of that ajax. And to write something like (variable1 + " " + variable2)Note: I want to overwrite, not append.

解决方案

Since you said you do not plan to host it anywhere you could just move your code into a Packaged app. From there you can use the chrome.fileSystem api.

Use chooseEntry() to ask the user which directory they want the app to be able to read/write to. In the callback check that the returned entry is valid, and then store it for later use.

var userDir = null;
chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(theEntry) {
   //do sanity check(s) and store it
   if(!theEntry.isDirectory) {
      //report error
      return;
   }
   userDir = theEntry;
});

Once you have a directory entry reference than you can use getFile() to get a reference to a file, creating it if it doesn't already exist, same goes for creating subdirectories just substitute getFile with getDirectory. Then use createWriter() get a FileWriter instance to write to that file.

function saveData(filename,data){
   if(!userDir) {
      //report error
      return;
   }

   userDir.getFile(filename, {create: true}, function(fileEntry) {
      if(!fileEntry || !fileEntry.isFile){
         //report error
         return;
      }

      fileEntry.createWriter(function(fileWriter) {
         fileWriter.onwriteend = function(e) {
            //report success
         };
         fileWriter.onerror = function(e) {
            //report error: e.toString()
         };

         //Create a Blob from the data and write it.
         var blob = new Blob([data], {type: 'text/plain'});
         fileWriter.write(blob);
      });
   });
}

//at some point after user has selected directory
saveData("log.txt","Some data");

Check the various documentations for error reporting and other necessities.

If you want to only ask the user once for the directory use retainEntry() to save an id of the directory entry. And use restoreEntry() to get the reference back of the directory. From there its just a matter of doing the steps in the saveData function. Check the documentation for other processes like reading the file.

这篇关于JavaScript / Ajax写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:14