简体   繁体   English

如何使用具有完整路径的JavaScript从服务器读取文本文件?

[英]How to read a text file from server using JavaScript with complete path?

I want load a remote file from a server. 我想从服务器加载远程文件。

This file is not in a web site. 该文件不在网站中。 So when I open a file I use 所以当我打开文件时

window.open('http://5.135.140.184:8080/tpi/documents/D773.JPG', '_blank'); 

This opens my picture. 这打开了我的照片。 But if I want to use a complete path, I use 但是如果我想使用完整的路径,我会使用

window.open("http://5.135.140.184:8080/var/lib/tomcat/webapps/tpi/documents/D773.JPG", '_blank'); 

In this case I have a 404 error ... How do I do? 在这种情况下,我会显示404错误...我该怎么办?

PSThe file D773.JPG is in the path var/lib/tomcat/webapps/tpi/documents PS文件D773.JPG位于路径var/lib/tomcat/webapps/tpi/documents

You can only access things via HTTP that are served by an HTTP (eg, "web") server which recognizes and handles the URL you give it. 您只能通过HTTP访问由HTTP服务器(例如,“ Web”)服务器提供服务的服务器,该HTTP服务器识别并处理您为其提供的URL。 It would be possible to configure an HTTP server to allow you to access any file by full path, but it would almost certainly be an extraordinarily bad idea to make **all* files on a machine accessible via HTTP. 可以配置HTTP服务器以允许您通过完整路径访问任何文件,但是几乎可以肯定的是,使机器上的“所有”文件都可以通过HTTP访问是一个非常糟糕的主意。

Your question contradicts itself by saying both that the file isn't in a web site and that you can access the file via http://5.135.140.184:8080/tpi/documents/D773.JPG . 您的问题与自己矛盾,因为它说该文件不在网站中,并且您可以通过http://5.135.140.184:8080/tpi/documents/D773.JPG访问该文件。 If you can access the file via that URL, then an HTTP server at 5.135.140.184:8080 is running and handles that URL by giving you the file. 如果您可以通过该URL访问文件,则位于5.135.140.184:8080的HTTP服务器正在运行,并通过向您提供文件来处理该URL。 That's what you should stick with. 那就是你应该坚持的。

In a comment you've added 在您添加的评论中

I want load an other file that is not in the web sites. 我要加载不在网站上的其他文件。

You can't, not via HTTP. 您不能,不能通过HTTP。 HTTP requires a web server that understands the URL. HTTP需要一个能够理解URL的Web服务器。 If your web server at 5.135.140.184:8080 isn't configured to deliver the file you want, you cannot access it via HTTP (without changing the server configuration). 如果未将位于5.135.140.184:8080的Web服务器配置为传递所需的文件,则无法通过HTTP访问它(无需更改服务器配置)。

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

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