简体   繁体   English

错误:不允许加载本地资源

[英]error: Not allowed to load local resource

I'm constructing the URL dynamically pointing to local IP address in my action class,我正在构建动态指向我的操作类中的本地 IP 地址的 URL,
Below is the URL constructed:下面是构造的URL:

<a file='#' onClick=window.open('file://154.66.111.123/SD/SPRD/index.htm','_self') >Click Here </a>

The above URL works fine in IE, but in chrome i could not able to access that URL, below is the exception noticed on browser console:上面的 URL 在 IE 中工作正常,但在chrome 中我无法访问该 URL,以下是浏览器控制台上注意到的异常:

Not allowed to load local resource:file://154.66.111.123/SD/SPRD/index.htm

How can i access local file system from chrome?Please suggest.我如何从 chrome 访问本地文件系统?请提出建议。 Thanks.谢谢。

You can't access a file like that for security reasons.出于安全原因,您无法访问这样的文件。 But you can access a file without uploading it using javascript.但是您可以访问文件而无需使用 javascript 上传它。 one way to do that is using file input field.一种方法是使用文件输入字段。

  1. Add a file input field to the page向页面添加文件输入字段
  2. Add onchange event listener to the inputonchange事件侦听器添加到输入
  3. Get the file获取文件
  4. Create the url using URL.createObjectURL function使用URL.createObjectURL函数创建 url

sample code示例代码

let input = document.querySelector("#input");

input.addEventListener("change", (e)=>{
        let file = input.files[0];
        let url = URL.createObjectURL(file); // A valid URL that you can use on the page

      })

file:// work only on your computer. file://仅适用于您的计算机。

It's a security.这是一种安全。

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

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