简体   繁体   English

无法在try / catch块中捕获Chrome的“无法加载本地资源”错误

[英]Cannot catch Chrome's 'Cannot Load Local Resource' error in try/catch block

I am attempting to open a local folder by setting window.location.href to file://folder/ , which I can do in IE, cannot in Chrome. 我试图通过将window.location.href设置为file://folder/来打开本地文件file://folder/ ,我可以在IE中执行,但不能在Chrome中。

My goal is to catch whenever a browser blocks local file access so I can call some other code. 我的目标是在浏览器阻止本地文件访问时catch以便我可以调用其他代码。 Chrome's console is showing me 'Not allowed to load local resource:...' but I am not able to catch it with a try/catch block Chrome的控制台向我显示'不允许加载本地资源:...'但我无法通过try/catch块捕获它

Attempt: 尝试:

  function OpenLocalResource() { try { //Fails in Chrome (expected) window.location.href = 'file://folder/whatever/'; } catch (err) { //Chrome gives script error 'Not allowed to load local resource:' //I am expecting to hit this catch block but it does not alert("Error hit!"); } } OpenLocalResource(); 

How can I detect when a browser does not allow local resource access? 如何检测浏览器何时不允许本地资源访问?

It's a security setting, I don't think you can catch it. 这是一个安全设置,我认为你不能抓住它。 But you could start chrome using the cmd prompt and adding --allow-file-access. 但您可以使用cmd提示符启动chrome并添加--allow-file-access。

To see if it's allowed on chrome: 要查看Chrome是否允许:

 function OpenLocalResource($file) { var img = $('<img>'); img.load(function() { console.log(this) }) img.error(function(err) { if(err.originalEvent.path[0].currentSrc.length == 0) { console.log('localfile fail',$file) } else { console.log('regular http/image format fail',$file, err); // Add here an eventual other check for incase a file DID get loaded, but it's format failed or something. // Usually you can check via $file == err.originalEvent.path[0].currentSrc //because chrome will turn C:\\userlog.txt into file:///C:/userlog.txt //whilst http:// image urls will remain the same. } }) img.attr('src',$file); } OpenLocalResource('C:\\\\userdata.txt'); OpenLocalResource('http://www.google.com'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Try this, but you may need to point the image src to an actual image on the client. 试试这个,但你可能需要将图像src指向客户端上的实际图像。

function noLocalAccess() {
    alert('Error hit!');
}
function OpenLocalResource() {
    var myImage=new Image();
    myImage.onerror=new Function("noLocalAccess()");
    myImage.src='file:///c:/';

    window.location.href = 'file://folder/whatever/';
}
OpenLocalResource();    

Use ajax to test if the file exists before using window.location. 在使用window.location之前,使用ajax测试文件是否存在。 I don't believe it is possible to catch the error any other way, because the location is set even with an error, and the JavaScript handler is lost from scope. 我不相信有可能以任何其他方式捕获错误,因为即使有错误也设置了位置,并且JavaScript处理程序从范围中丢失。

var xhr = new XMLHttpRequest();
xhr.open('GET', '/folder/whatever/myfile.html', true);
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            window.location.href = '/folder/whatever/myfile.html';
        } else {
            alert("Error hit!");
        }
    }
};

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

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