javascript 如何从客户端访问和读取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10776221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access and read a file from client-side?
提问by Lajos Arpad
I have a file foo.txt with the following rows:
我有一个包含以下行的文件 foo.txt:
row1
row2
row3
I would like to create a link to this file in my HTML and to read it with Javascript on client-side. Is there a solution for this?
我想在我的 HTML 中创建一个指向此文件的链接,并在客户端使用 Javascript 读取它。有解决方案吗?
回答by Fluidbyte
if the file is on your server you can use the following:
如果文件在您的服务器上,您可以使用以下命令:
jQuery.get('your_file.txt',function(data){
alert(data);
});
If you want to pull in from your local system you'll need an upload mechanism in place first.
如果你想从你的本地系统中拉进来,你首先需要一个上传机制。
Update:
更新:
Do it synchronously:
同步进行:
$.ajax({
url: "your_file.txt",
async: false,
success: function(data){ alert(data); }
});
回答by Sarfraz
File Not on your own server:
文件不在您自己的服务器上:
That is not possible with client-side JavaScript due to security reasons. Imagine you are trying to access the file system of the client.
出于安全原因,这在客户端 JavaScript 中是不可能的。想象一下,您正在尝试访问客户端的文件系统。
Though there is option of File System Object in Internet Explorer but yet it is generally not a good idea to do I/O on client machine unless there is no purpose to harm it.
虽然 Internet Explorer 中有文件系统对象的选项,但通常在客户端机器上进行 I/O 并不是一个好主意,除非没有任何目的。
File on your server:
服务器上的文件:
If however you meant to read a file from your own domain/space, you can use AJAXas @Fluidbyte pointed out.
但是,如果您打算从自己的域/空间读取文件,则可以使用AJAX,如@Fluidbyte 指出的那样。
You can also use File API of html5in browsers that support it.
您也可以在支持它的浏览器中使用html5 的 File API。
回答by Joseph
You could use the File APIfor supported browsers.
If not, use the traditional "upload to server and read from server" approach. This can be done in AJAX as well.
如果没有,请使用传统的“上传到服务器并从服务器读取”方法。这也可以在 AJAX 中完成。
回答by Ekim
I would suggest you store the file in HTML5 Localstorage in the Clients browser since it is rows you could save it in a simple loop as
我建议您将文件存储在客户端浏览器的 HTML5 Localstorage 中,因为它是行,您可以将其保存在一个简单的循环中
localStorage.setItem("key1", "value1");
localStorage["key1"] = "value1";
...
write a serversided script that reads it line by line and outputs javascript code.
编写一个服务器端脚本,逐行读取并输出 javascript 代码。
then later to read it you simply need to :
然后稍后阅读它,您只需要:
function listAllItems(){
for (i=0; i<=localStorage.length-1; i++)
{
key = localStorage.key(i);
val = localStorage.getItem(key);
}
}
Each website usually gets 5MB of Localstorage space. You can write a function to update the Localstorage append something to it and send it back to the server if needed, etc.
每个网站通常会获得 5MB 的 Localstorage 空间。您可以编写一个函数来更新 Localstorage 附加一些东西,并在需要时将其发送回服务器等。
回答by Nilesh Savaliya
var oFReader = new FileReader();
oFReader.readAsText(document.getElementById("fileImportOrExport").files[0]);
oFReader.onload = function (oFREvent) {
var fileImportOrExportContent = oFREvent.target.result;
}
oFReader.onerror = function (OFREvent) {
alert("Error in reading");
}