【问题标题】:Cannot catch Chrome's 'Cannot Load Local Resource' error in try/catch block在 try/catch 块中无法捕获 Chrome 的“无法加载本地资源”错误
【发布时间】:2016-02-10 21:13:06
【问题描述】:

我正在尝试通过将window.location.href 设置为file://folder/ 来打开本地文件夹,我可以在 IE 中执行此操作,但在 Chrome 中无法执行。

我的目标是在浏览器阻止本地文件访问时catch,以便我可以调用其他代码。 Chrome 的控制台显示“不允许加载本地资源:...”,但我无法使用 try/catch 块捕获它

尝试:

 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();

如何检测浏览器何时不允许本地资源访问?

【问题讨论】:

    标签: javascript google-chrome try-catch


    【解决方案1】:

    这是一个安全设置,我不认为你能抓住它。但是您可以使用 cmd 提示符启动 chrome 并添加 --allow-file-access。

    【讨论】:

      【解决方案2】:

      查看 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');
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

      【讨论】:

      • 这对我有用。注意:我必须修改条件以检查err.originalEvent.srcElement.src,因为err.originalEvent.path 未定义
      • 你可能想添加一个检查它是否设置然后检查我提供的那个。其他方式适合您的方式。基本上前提是chrome不会设置currentsrc元素,如果它是一个不允许的本地文件
      【解决方案3】:

      试试这个,但您可能需要将图像 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();    
      

      【讨论】:

        【解决方案4】:

        在使用 window.location 之前使用 ajax 测试文件是否存在。我不相信有可能以任何其他方式捕获错误,因为即使出现错误也会设置位置,并且 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!");
                }
            }
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-05
          • 2019-01-12
          • 2023-01-14
          • 1970-01-01
          • 2023-01-18
          相关资源
          最近更新 更多