2017-09-02 68 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

但隨後也不工作 –

相關問題