简体   繁体   中英

error: Not allowed to load local resource

I'm constructing the URL dynamically pointing to local IP address in my action class,
Below is the URL constructed:

<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:

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. Thanks.

You can't access a file like that for security reasons. But you can access a file without uploading it using 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 input
  3. Get the file
  4. Create the url using URL.createObjectURL function

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.

It's a security.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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