简体   繁体   English

Firefox上的Javascript打开文件

[英]Javascript open File on Firefox

Is it possible to open a file with Javascript? 是否可以使用Javascript打开文件?

Its all local so I have a index.html in which the javascript should read a text file which is in the same direction called readText.txt 它全部是本地的,所以我有一个index.html,其中javascript应该读取一个文本文件,该文件的方向相同,称为readText.txt

I heard that there are some security restrictions but if its all local shouldn't it work? 我听说有一些安全限制,但是如果它在所有本地都不起作用? If no is there any workaround? 如果没有,有什么解决方法? I've googled some and found the Mozilla FileHandleApi but It doesn't work for me 我已经在Google上进行了搜索,找到了Mozilla FileHandleApi,但它对我不起作用

JS is executed on client, client has no notion of server files. JS是在客户端上执行的,客户端没有服务器文件的概念。 But you can use AJAX to request a file if you have URL. 但是,如果您具有URL,则可以使用AJAX请求文件。 You can do something like $.get('readText.txt') (jQuery) from index.html. 您可以从index.html执行类似$.get('readText.txt') (jQuery)的操作。

FileHandleApi is used to manipulate client files. FileHandleApi用于处理客户端文件。

It is important to understand what is server here and what is client and where what is executed. 重要的是要了解什么是服务器,什么是客户端以及在何处执行。 Server is a machine that serves pages (and files). 服务器是提供页面(和文件)的机器。 Client is the machine where the browser runs. 客户端是运行浏览器的机器。 Server and client communicate via network. 服务器和客户端通过网络进行通信。 You can run server on your local machine but this doesn't change the nature of the relationship. 您可以在本地计算机上运行服务器,但这不会改变关系的性质。

The short answer is no, what you're attempting will not work. 简短的答案是“否”,您尝试执行的操作将不起作用。

The longer answer: That's not how the web works, even on your local machine. 更长的答案:即使在您的本地计算机上,网络也不是这样工作的。 Its a strictly client/server relationship: your browser doesn't (and shouldn't) care that the client and the server in this case are the same machine. 这是严格的客户端/服务器关系:在这种情况下,您的浏览器无需(也不应该)关心客户端和服务器是同一台机器。 As far as your browser knows, that readme.txt file is on the 'server' and needs to be fetched with an AJAX call. 据您的浏览器所知,该readme.txt文件位于“服务器”上,需要通过AJAX调用来获取。 The browser (for the security reasons you mentioned) can only (for our purposes here) manipulate client files it creates. 浏览器(出于您提到的安全原因)仅(出于此处的目的)只能操纵其创建的客户端文件。 The filehandle API is for that purpose. filehandle API就是用于此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM