2014-12-03 96 views
0

工作我寫的JAX-RS服務下載的文件與Apache服務器:JAX-RS Web服務通過瀏覽器,但不通過XMLHTTPRequest的

服務的主要內容是 -

@GET 
@Produces("application/pdf") 
public Response convertCtoF() { 

    String path = "D:\\a.pdf"; 
    File file=new File(path); 
    ResponseBuilder rb = Response.ok((Object) file); 
     rb.header("Content-Disposition","attachment; filename=a.pdf"); 
     return rb.build(); 
} 

這是工作當我通過網絡瀏覽器訪問它時。

但是,當我通過XMLHTTPRequest訪問它不起作用。它給XMLHttp.Status = 0

客戶端代碼:

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function Download() 
{ 

var xmlhttp=new XMLHttpRequest(); 


xmlhttp.open('GET','http://localhost:8080/RESTExample/ABC/ctofservice',true); 



xmlhttp.send(); 
    xmlhttp.onreadystatechange = function() { 


       if (xmlhttp.readyState == 4) { 
       alert('xmlhttp.readyState == 4'); 

        if (xmlhttp.status == 200) { 

         alert('xmlhttp.status == 200'); 
        } 
        else 
        { 
        alert(xmlhttp.status); 
        } 

        } 
       } 


} 


</script> 
</head> 
<body> 

<h2>File Downloading Web Interface</h2> 
<div id="myDiv"></div> 
<button type="button" onclick="Download()">Download File</button> 

</body> 
</html> 

回答

0

因爲你下載文件,並出於安全原因,你不能這樣做與阿賈克斯它不使用XMLHttpRequest工作。有各種解決方法可供嘗試。我不會在這裏複製這些內容,因此請嘗試在Google上搜索「ajax文件下載」,然後選擇您偏好的方法。