Javascript 如何从 Firefox 11 中的 http://localhost 服务页面查看 file:// 图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10270701/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:36:11  来源:igfitidea点击:

How do I view file:// images from http://localhost served pages in Firefox 11?

javascripthtmlfilefirefoxlocal

提问by Michael

I've got a server running on my localhost machine (port 8080) that is trying to create IMG tags to local files. But, the files do not appear in Firefox 11.

我有一台在我的本地主机(端口 8080)上运行的服务器,它试图为本地文件创建 IMG 标签。但是,这些文件不会出现在 Firefox 11 中。

I was originally create the image tag using Javascript, but since that failed I fell back to HTML to experiment with various ways of directly referencing the file. None of the following work:

我最初是使用 Javascript 创建图像标签,但由于失败了,我又回到 HTML 来尝试各种直接引用文件的方法。以下均无效:

<img src="file:///localhost:8080/C://Users/me/IMG_1000.JPG">
<img src="file:///localhost/C://Users/me//IMG_1000.JPG">
<img src="file:///C://Users/me/IMG_1000.JPG">

If I paste the last URL directly into the address bar, the image opens fine.

如果我将最后一个 URL 直接粘贴到地址栏中,则图像打开正常。

I have followed the directions here:

我已按照此处的说明进行操作:

http://kb.mozillazine.org/Firefox_:_Issues_:_Links_to_Local_Pages_Don%27t_Work

http://kb.mozillazine.org/Firefox_:_Issues_:_Links_to_Local_Pages_Don%27t_Work

I have tried modifying both user.js, and prefs.js, both directly and via the about:config viewer.

我已经尝试直接和通过 about:config 查看器修改 user.js 和 prefs.js。

I got the correct path to the profile directory from Help -> Troubleshooting Information -> Profile Directory -> Open Containing Folder.

我从帮助 -> 故障排除信息 -> 配置文件目录 -> 打开包含文件夹获得了配置文件目录的正确路径。

The relevant lines added are:

添加的相关行是:

user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");
user_pref("capability.policy.localfilelinks.sites", "localhost:8080");
user_pref("capability.policy.policynames", "localfilelinks");

I originally put http:// in from of "localhost:8080", but Firefox removed that. I also tried just "http://localhost" and "http://127.0.0.1", but those did not work either.

我最初将 http:// 放在 "localhost:8080" 中,但 Firefox 删除了它。我也只试过“http://localhost”和“http://127.0.0.1”,但这些也不起作用。

BUT... strangely enough, even though I entered them from about:config and can see them in prefs.js, I cannot see them from about:config! This makes me think that perhaps the information at mozillazine is out of date, after all it says the info is for "Firefox 1.5 and above" and I am using Firefox 11!

但是...奇怪的是,即使我从 about:config 输入它们并且可以在 prefs.js 中看到它们,但我无法从 about:config 看到它们!这让我觉得可能 mozillazine 上的信息已经过时了,毕竟它说信息是针对“Firefox 1.5 及更高版本”的,而我使用的是 Firefox 11!

Note: the reason I want to use file:// access is that I don't want to allow arbitrary access to the files on my machine. To explain a bit more: I've got an interface to a database, and all that is required to access the database is a password. That's fine because I want to allow database access outside my machine to anyone with the password, but the image files exist outside the database. In order to server up the files through the server (because the files that exist are dynamic) I would need to create a new CGI that takes some kind of identifier in the database, but since the user can access the DB they could inject any filename they wanted.

注意:我想使用 file:// 访问的原因是我不想允许任意访问我机器上的文件。再解释一下:我有一个数据库接口,访问数据库所需的只是一个密码。这很好,因为我想允许任何拥有密码的人在我的机器之外访问数据库,但图像文件存在于数据库之外。为了通过服务器提供文件(因为存在的文件是动态的),我需要创建一个新的 CGI,它在数据库中使用某种标识符,但由于用户可以访问数据库,因此他们可以注入任何文件名他们想。

Edit: Also, I can't window.open() the file URL either. What possible security benefit could that have?

编辑:另外,我也不能 window.open() 文件 URL。这有什么可能的安全好处?

采纳答案by jesup

<img src="file:///localhost:8080/C://Users/me/IMG_1000.JPG">
<img src="file:///localhost/C://Users/me//IMG_1000.JPG">

If it's a server on 8080, you shouldn't be using file:// -- It's not a file. It should be http://localhost:8080/blah. I don't think it works this way, but the second (if it should work) would be file://localhost/c:/* - note the number of slashes. And the line above has an additional // where it should be /

如果它是 8080 上的服务器,则不应使用 file://——它不是文件。它应该是http://localhost:8080/blah。我不认为它以这种方式工作,但第二个(如果它应该工作)将是 file://localhost/c:/* - 注意斜线的数量。上面的行有一个额外的 // 应该在的位置 /

<img src="file:///C://Users/me/IMG_1000.JPG">

Now that makes sense, though for that you'll be limited in the ability to access it because of the security restrictions.

现在这是有道理的,但由于安全限制,您将限制访问它的能力。

user_pref("capability.policy.localfilelinks.sites", "localhost:8080");

That (probably) should work the the file:///c:/* stuff if the page is loaded from http://localhost:8080/*.

如果页面是从http://localhost:8080/*加载的,那(可能)应该可以处理 file:///c:/* 的内容。

  • use http://localhost:8080/* - probably the best solution
  • Use a folder and make it all soft or hardlinks to the actual files to minimize disk usage and update issues.
  • Use a folder and synchronize using any one of a myriad of tools (rsync, etc)
  • 使用http://localhost:8080/* - 可能是最好的解决方案
  • 使用文件夹并使其全部软链接或硬链接到实际文件,以最大限度地减少磁盘使用和更新问题。
  • 使用文件夹并使用无数工具(rsync 等)中的任何一种进行同步

回答by Parv Sharma

  1. make a new folder inside ur website
    2 copy all the data in the new folder
    3.then provide the link to that folder.
  1. 在您的网站内创建一个新文件夹
    2 复制新文件夹中的所有数据
    3.然后提供指向该文件夹的链接。