文件操作,如使用Javascript而无需服务器读取/写入本地文件 [英] File manipulations such as Read/Write local files using Javascript without server

查看:151
本文介绍了文件操作,如使用Javascript而无需服务器读取/写入本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在尝试使用java脚本的文件操作系统。正如我从W3C File API( https://www.w3.org/TR/FileAPI/ ),我们只能读取本地文件,如

  var file =test.txt; 
函数readTextFile(file){
var readFile;
if(window.XMLHttpRequest){
//用于新浏览器
readFile = new XMLHttpRequest();
} else {
//用于IE5或IE6等旧浏览器
readFile = new ActiveXObject(Microsoft.XMLHTTP);
}
readFile.open(GET,file,true);
readFile.onreadystatechange = function(){
if(readFile.readyState === 4){
if(readFile.status === 200 || readFile.status == 0){
//将显示从文件读取的文本
console.log(readFile.responseText);
}
}
}
readFile.send(null);
}

但没有选项可以在没有服务器的情况下写入文件。我曾尝试从 http://www.stackoverflow.com/ 等网站获取解决方案,该研究表示几乎没有可能性。



例如,我从

。 github.com/Arahnoid/9925725rel =nofollow noreferrer> https://gist.github.com/Arahnoid/9925725



显示错误TypeError:file.open不是一个函数。



所以我的问题是,是否有可能对文件进行操作(仅询问Write文件)没有使用服务器端脚本的本地文件,或者是任何可用的扩展?



我们可以使用服务器脚本语言如PHP,Node.js 。

在此先感谢。

解决方案

在您的代码中,不从本地文件读取( test.txt ),它向服务器发送Ajax GET 请求并读取文件在服务器端。



要读取本地文件(存储在安装了浏览器的机器中的文件),您需要使用FileAPI,该文件在当前代码中未使用。 / p>

要将文件写入本地,不可能直接使用JavaScript编写文件。否则,这将是一个巨大的安全漏洞。但是,您可以从File对象生成一个URL,并使用该URL作为< a> href 标签,以便用户可以下载并手动写入本地。



这里是一段代码片段,用于阅读& 写入本地文件:



  var inputElement = document.getElementById(input ); var reader = new FileReader(); var downloadLink = document.getElementById('downloadLink'); reader.onloadend = function(){console.log(reader.result);} inputElement.addEventListener(change,handleFiles ,false);函数handleFiles(){var fileSelected = this.files [0]; / *现在你可以使用文件列表* / reader.readAsBinaryString(fileSelected); downloadLink.href = window.URL.createObjectURL(fileSelected);}  

< input type =fileid =input>< a id =downloadLinkdownload>下载< / a>


I'm just trying on the task, file manipulation system using java script. As I was referred from W3C File API( https://www.w3.org/TR/FileAPI/ ), we can only read local files like

var file = "test.txt";
function readTextFile(file) {
   var readFile;    
   if(window.XMLHttpRequest){
      // for new browsers
      readFile = new XMLHttpRequest();
   }else{
      //for old browsers like IE5 or IE6
      readFile = new ActiveXObject("Microsoft.XMLHTTP");
   }    
   readFile.open("GET", file, true);
   readFile.onreadystatechange = function() {
      if(readFile.readyState === 4) {
         if(readFile.status === 200 || readFile.status == 0) {
            //text will be displayed that read from the file
            console.log(readFile.responseText);
         }
      }
   }
   readFile.send(null);
}

but it looks there is no options to write on file without server. I was tried to fetch solutions from the websites like http://www.stackoverflow.com/, the study says almost there is no possibilities.

For an example what I got is

from https://gist.github.com/Arahnoid/9925725

It shows error "TypeError: file.open is not a function."

So my question is, Is there any possible to file manipulations(asking only about Write file) for local files without using server-side scripting or is any extensions like available?

We can do file manipulations using server scripting languages such as PHP, Node.js.

Thanks in advance.

解决方案

In your code, it's not reading from the local file (test.txt), it's sending Ajax GET request to server and read file in server side.

To read local file (files that stored in machine where browser is installed), you need to use FileAPI, which is not used in current code.

To write file to local, it's impossible to write it directly using JavaScript. Otherwise, it would be a huge security vulnerability. However, you can generate a URL from File object, and use that URL as the href attribute of <a> tag, so that user can download and "write to local" manually.

Here is a code snippet to read & "write" local file:

var inputElement = document.getElementById("input");
var reader = new FileReader();
var downloadLink = document.getElementById('downloadLink');

reader.onloadend = function(){
  console.log(reader.result);
}
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  var fileSelected = this.files[0]; /* now you can work with the file list */
  reader.readAsBinaryString(fileSelected);
  downloadLink.href = window.URL.createObjectURL(fileSelected);
}

<input type="file" id="input">
<a id="downloadLink" download>Download</a>

这篇关于文件操作,如使用Javascript而无需服务器读取/写入本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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