Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

javascript - access to iframe contents of local file

I have the file file:///C:/index.html that has an iframe inside it:

<iframe id="folder_browser" src="file:///C:/Files/"></iframe>

the contents of the Files folder load into the iframe but I can not access to them by javascript or jquery. what can I do?

I have tried $('#folder_browser').contents().find('body').html(); but no luck!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cause "Same origin policy" you can't get the content of an iframe either in this kind of situations nor in the case you have the main page from a domain and the iframe document from another domain.

"Same origin policy" is a set of restrictions that browsers apply to webpages to prevent communicating each other. These restrictions prevent a lot of security issues from internet web sites like accessing to your private data stored on your local machine using tricks like that one.

Nevertheless there are some ways (and conditions) to bypass these restrictions:

  1. window.document.domain variable manipulation
  2. Proxy
  3. Cross Document Messaging

But this is possible only if both pages are into the same domain and by modifying the content of both html pages (in particular their javascripts) so pages can authorize each other communications. This can be only done if both the parent page and the iframe child page are web pages: in this case the address file:///C:/something/ is not a web address, it is a local directory.

It is possible to find some interesting tutorials about same origin policy at

Bypass Same Origin Policy ,

and

Same-origin policy for file: URIs .

In this last one in particular you can read "The new security.fileuri.strict_origin_policy preference, which defaults to true, can be set to false if the user doesn't want to strictly enforce the same origin policy on file: URIs."

Another interesting information resource is:

Bypassing the Same-Origin-Policy For Local Files During Development .

In which I found the following hint that may help:

The only real cross-browser solution to circumvent the problem altogether is to serve the content with a local webserver, so that you can access it through HTTP.

In other words you have to install a web server software into your computer and configure it to serve files under an address like http:///yourlocalwebserverdirectory.

Nevertheless you can get files (with the interaction of the user) by using the html input type file, here a working example:

document.getElementById('loadfile').onchange = function(event) {
  var files = loadfile.files;
  var len = files.length;
  var outpt = document.getElementById('filelist');
  
   for (var i=0; i<len; i++)
     {
       outpt.innerHTML += files[i].name + '<br>';
     }
 }
<input type="file" id="loadfile" name="files[]" multiple = "multiple" title="load one or more files" autofocus="autofocus" />

<div id="filelist"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...