为什么我不能执行 ?
如果 html 文件在本地(在我的 C 驱动器上),则它可以工作,但如果 html 文件在服务器上并且图像文件在本地,则不行。这是为什么?
有什么可能的解决方法吗?
It works if the html file is local (on my C drive), but not if the html file is on a server and the image file is local. Why is that?
Any possible workarounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果客户端可以请求本地文件系统文件,然后使用 JavaScript 找出其中的内容,这将是一个安全漏洞。
解决这个问题的唯一方法是在浏览器中构建扩展。 Firefox扩展和IE扩展可以访问本地资源。 Chrome 的限制要严格得多。
It would be a security vulnerability if the client could request local file system files and then use JavaScript to figure out what's in them.
The only way around this is to build an extension in a browser. Firefox extensions and IE extensions can access local resources. Chrome is much more restrictive.
难道您不应该使用
file://C:/localfile.jpg
而不是C:/localfile.jpg
吗?Shouldn't you use
file://C:/localfile.jpg
instead ofC:/localfile.jpg
?除非您访问本地 html 页面,否则浏览器不允许访问本地文件系统。您必须将图像上传到某个地方。如果它与 html 文件位于同一目录中,则可以使用
Browsers aren't allowed to access the local file system unless you're accessing a local html page. You have to upload the image somewhere. If it's in the same directory as the html file, then you can use
<img src="localfile.jpg"/>
C:
不是可识别的 URI 方案。尝试使用file://c|/...
代替。C:
is not a recognized URI scheme. Tryfile://c|/...
instead.老实说,最简单的方法是将文件托管添加到服务器。
打开 IIS
在默认网站下添加虚拟目录
为 C: 驱动器上的文件夹添加适当的权限“NETWORK SERVICE”和“IIS AppPool\DefaultAppPool”
刷新默认网站
现在您可以浏览到。通过导航到 http://yourServerName/whateverYourFolderNameIs/yourImage.jpg 并使用该 url 来访问该文件夹中的任何图像在您的 img src 中
希望这对某人有帮助
Honestly the easiest way was to add file hosting to the server.
Open IIS
Add a Virtual Directory under Default Web Site
Add the appropriate permissions to the folder on the C: drive for "NETWORK SERVICE" and "IIS AppPool\DefaultAppPool"
Refresh Default Web Site
And you're done. You can now browse to any image in that folder by navigating to http://yourServerName/whateverYourFolderNameIs/yourImage.jpg and use that url in your img src
Hope this helps someone
我们可以使用JavaScript的
FileReader()
及其`readAsDataURL(fileContent函数来显示本地驱动器/文件夹文件。将更改事件绑定到图像,然后调用JavaScript的showpreview函数。We can use JavaScript's
FileReader()
and its `readAsDataURL (fileContent function to show local drive/folder file. Bind change event to image then call JavaScript's showpreview function.IE 9:如果您希望用户在将图像发布到服务器之前查看图像:
用户应将该网站添加到“可信网站列表”中。
IE 9 : If you want that the user takes a look at image before he posts it to the server :
The user should ADD the website to "trusted Website list".
撇开 Newtang 对安全规则的观察不谈,您如何知道查看您页面的任何人都会在
c:\localfile.jpg
中看到正确的图像?你不能。即使你认为你可以,你也不能。一方面,它预设了 Windows 环境。Newtang's observation about the security rules aside, how are you going to know that anyone who views your page will have the correct images at
c:\localfile.jpg
? You can't. Even if you think you can, you can't. It presupposes a windows environment, for one thing.我看到您尝试执行的操作有两种可能性:
您希望在服务器上运行的网页在您最初设计它的计算机上找到该文件吗?
您希望它从正在查看该页面的电脑上获取它吗?
选项 1 没有意义:)
选项 2 将是一个安全漏洞,浏览器禁止网页(从网络提供)在查看者的计算机上加载内容。
凯尔·哈德森(Kyle Hudson)告诉了你需要做什么,但这太基本了,我很难相信这就是你想做的一切。
I see two possibilities for what you are trying to do:
You want your webpage, running on a server, to find the file on the computer that you originally designed it?
You want it to fetch it from the pc that is viewing at the page?
Option 1 just doesn't make sense :)
Option 2 would be a security hole, the browser prohibits a web page (served from the web) from loading content on the viewer's machine.
Kyle Hudson told you what you need to do, but that is so basic that I find it hard to believe this is all you want to do.
如果你使用 Google chrome 浏览器,你可以这样使用
,但是如果你使用 Mozila Firefox,你需要添加“文件” ex。
if you use Google chrome browser you can use like this
But if you use Mozila Firefox the you need to add "file " ex.
以
file:///
开头并以filename
结尾应该可以:starts with
file:///
and ends withfilename
should work:您还需要上传图像,然后链接到服务器上的图像。
You need to upload the image aswell, then link to the image on the server.
如果您只是为自己或某些客户部署本地网站,则可以通过在 cmd 中以管理员身份在网站根目录中运行
mklink /D MyImages "C:/MyImages"
来解决此问题。然后在 html 中执行,mklink 建立的符号链接会将相对的 src 链接与 C 盘上的链接连接起来。它为我解决了这个问题,因此它可能会帮助其他提出这个问题的人。
(显然这不适用于公共网站,因为你无法轻松地在人们的计算机上运行 cmd 命令)
If you're deploying a local website just for yourself or certain clients, you can get around this by running
mklink /D MyImages "C:/MyImages"
in the website root directory as an admin in cmd. Then in the html, do<img src="MyImages/whatever.jpg">
and the symbolic link established by mklink will connect the relative src link with the link on your C drive. It solved this issue for me, so it may help others who come to this question.(Obviously this won't work for public websites since you can't run cmd commands on people's computers easily)
我尝试了很多技术,终于在C#端和JS端找到了一种。
您无法为 src 属性提供物理路径,但可以将 base64 字符串作为 Img 标记的 src 提供。
让我们看一下下面的 C# 代码示例。
C# 代码
希望这会有所帮助
I have tried a lot of techniques and finally found one in C# side and JS Side.
You cannot give a physical path to src attribute but you can give the base64 string as a src to Img tag.
Lets look into the below C# code example.
C# code
Hope this will help
让图像由用户选择怎么样?使用 input:file 标签,然后在选择图像后将其显示在客户端网页上?这对于大多数事情都是可行的。现在我正在尝试让它适用于 IE,但与所有微软产品一样,它是一个 cluster fork()。
what about having the image be something selected by the user? Use a input:file tag and then after they select the image, show it on the clientside webpage? That is doable for most things. Right now i am trying to get it working for IE, but as with all microsoft products, it is a cluster fork().
如果您想在本地添加文件作为背景到您的网站。
If you want to add a file as background to your website locally.