2017-09-02 70 views
-1
  • 我试图找到我的文件的大小,当我粘贴在texbox的URL。
  • 我的代码适用于一个场景,但是对于其他场景的破解。
  • 你能告诉我如何解决它。
  • 提供下面的代码。
  • 我可以从JQuery的

实现使用html5rocks.com/en/tutorials/cors根据本条CORS工作输入XMLHttpRequest无法加载http://github/sample.pdf

http://localhost:1200/sample.pdf

工作不输入

http://github/sample.pdf

错误: 运动:1 XMLHttpRequest无法加载http://github/sample.pdf。请求的资源上没有“Access-Control-Allow-Origin”标题。因此'Origin'http://localhost:1200'不允许访问。

C:\桌面\ sample.pdf

错误:XMLHttpRequest的无法加载文件:/// C:/Desktop/sample.pdf。协议方案仅支持跨源请求:http,data,chrome,chrome-extension,https。

<!DOCTYPE html> 
<html> 
<head> 
<link rel="stylesheet" src="test.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 

<script> 

</script> 
</head> 
<body> 


<div id="win1"> 
    <p>First Window</p> 
    <input type="file" name="batchFile" id="batchFile" title="Select file" /> 

    <input type="text" name="fname" id="fname"> 
    <button type="button" id="save">save</button> 


<script> 


$(document).ready(function() { 

    // Listen to save button click 
    $("#save").click(function() { 
    // Set the persistent value 
    testingWindowVal = $("#fname").val(); 
    // Size calculate 
    var request; 
    request = $.ajax({ 
     type: "HEAD", 
     url: $("#fname").val(), 
     success: function() { 
     alert("Size is " + request.getResponseHeader("Content-Length")); 
     // 10485760 -10MB 
     if(request.getResponseHeader("Content-Length") <= 10485760/2){ 
      alert(request.getResponseHeader("Content-Length") <= 10485760/2); 
      $('.testingConfirmation').show(); 
     } 
     else{ 
      alert(request.getResponseHeader("Content-Length") <= 10485760/2); 
     } 
     } 
    }); 
    // Show the dialog 
    /*if (testingWindowVal) { 
      $('.testingConfirmation').show(); 
    }*/ 
    }); 



}); 


    </script> 

</body> 
</html> 
+0

是的,这就是所谓的跨域请求。除非您明确查询的服务器允许,否则您无法从浏览器执行此操作。你可以从你的服务器上完成,并将结果发送到浏览器。关于'C:/ Desktop/sample.pdf',出于明显的安全原因,浏览器不允许Javascript访问本地文件系统。 –

+0

@shaochuancs所以我可以实现使用https://www.html5rocks.com/en/tutorials/cors/根据这一节CORS从JQuery –

回答

0

我认为你需要写这样的事情你的道路是错误的

http://github.com/repo_Path/sample.pdf 

,而不是

http://github/sample.pdf 
+0

但随后也不工作 –

相关问题