我在服務器端使用REST球衣,在客戶端使用AngularJS。從REST Web服務下載zip文件+ AngularJS
我的要求是下載特定日期範圍的客戶端請求的zip文件。
服務器端代碼://時間是我已經硬編碼了一個zip文件,用於測試
@POST
@Path("/LogRange")
@Produces({MediaType.APPLICATION_OCTET_STREAM})
@Consumes({ MediaType.APPLICATION_JSON})
public Response getLogsBetween(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorization,
@Context HttpServletRequest request, @Context HttpServletResponse response, LogFolders folders){
StreamingOutput stream = new StreamingOutput(){
@Override
public void write(OutputStream arg0) {
// TODO Auto-generated method stub
BufferedOutputStream bus = new BufferedOutputStream(arg0);
try {
File file = new File("C:\\ProgramData\\ABC\\Logfiles\\UI.zip");
FileInputStream fizip = new FileInputStream(file);
byte[] buffer2 = IOUtils.toByteArray(fizip);
bus.write(buffer2);
bus.flush();
bus.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
return Response.status(Response.Status.OK).entity(stream).header("Content-Disposition","attachment; filename=\"log.zip\"").build();
}
客戶端代碼:
$http({
url : urlBase + endPoint,
method: "POST",
data: formData, //this is your json data string
headers : {
'Content-Type' : "application/json",
'Authorization' : authCode,
},
responseType: 'arraybuffer'
}).success(function(response, status, headers, config) {
var blob = new Blob([response], { type: "application/zip" });
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}).error(function (data, status, headers, config) {
//upload failed
});
文件獲取下載在本地,但它總是損壞和顯示器如下所示。 請幫助我如何正確下載文件。
可能,這將有助於http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery –