简体   繁体   English

如何在 Javascript 中读取具有固定路径的本地文件? 我只有文件的位置作为字符串。 不允许使用 HTML 表单

[英]How can I read a local file with a fixed path in Javascript? I only have the location of the file as a string. NO HTML forms allowed

I am creating a web app where I already know the location of a file that needs to be uploaded.我正在创建一个网络应用程序,我已经知道需要上传的文件的位置。 Note that all I have is the path (example: Users/username/Downloads or Users/username/Documents) or something like that.请注意,我所拥有的只是路径(例如:用户/用户名/下载或用户/用户名/文档)或类似的东西。 How can I get Javascript to read that file as a File object or as a binary string so that I can POST it to a server.如何让 Javascript 将该文件作为 File 对象或二进制字符串读取,以便将其发布到服务器。

Additionally, this file is not a simple file like a JSON or a txt file.此外,此文件不是 JSON 或 txt 文件等简单文件。 This file would usually be a Microsoft Word file.该文件通常是 Microsoft Word 文件。

Due to security reasons, JavaScript executed from web apps have very limited access to the file system.由于安全原因,从 Web 应用程序执行的 JavaScript 对文件系统的访问非常有限。

The best you can do is have the user select a file via a file input .您能做的最好的事情就是让用户通过文件输入选择一个文件。

If having the web app user install a browser extension is acceptable, a browser extension may have more access to the user's file system.如果让 Web 应用程序用户安装浏览器扩展是可以接受的,则浏览器扩展可能对用户的文件系统具有更多访问权限。 Here is an example extension that accesses a user's file system more directly: https://github.com/buggyj/savetiddlers这是一个更直接地访问用户文件系统的示例扩展: https ://github.com/buggyj/savetiddlers

I think you can't to read the any files from the front-end by javascript, It is not allowed to do so.我认为您不能通过 javascript 从前端读取任何文件,这是不允许的。 But you can get the file path from front-end and then pass it to the app framwork or back-end to processing.但是您可以从前端获取文件路径,然后将其传递给应用程序框架或后端进行处理。 Beacuse the app's framework or back-end has the ability to access system files.因为应用程序的框架或后端具有访问系统文件的能力。 Whereafter, you can POST to the contents of the file to the web server.之后,您可以将文件的内容 POST 到 Web 服务器。

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

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