我正在尝试使用ajax和servlet下载文件,但我得到的最大值是,我得到文件parseado
我有servlet的这一部分:
else if(type.equals("downloadDocument")){
String file = request.getParameter("filePath");
File f = new File(file);
if (f.exists() && f.isFile()){
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > -1){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
那个电话:
$.ajax({
type : "POST",
url : "./ServletDocuWindow?downloadDocument",
data : datos,
success : function(r) {
}
});
答案 0 :(得分:1)
我没有从AJAX下载文件,而是在新的单独窗口上传递请求。然后,我的servlet被调用,文件被下载到我的本地。此外,文件开始下载时关闭新窗口
我想你需要添加以下代码,你应该好好去。
response.setContentType("application/octet-stream");
response.setContentLength((int) downloadFile.length());
// set headers for the response
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", AppUtility.getConvertedString(fileName)); //to ensure that there are no space in the file name
response.setHeader(headerKey, headerValue);
答案 1 :(得分:0)
根据@dsp_user的建议,我用a来调用servlet,它运行良好
var doc = document.getElementById("windowDocumentId").value;
var index = $("#lvDocuments").data("kendoListView").select().index();
var link = document.createElement("a");
link.download = $("#lvDocuments").data("kendoListView").dataSource.view()[index].itemText;
link.href = "./ServletDocuWindow?" + doc + "," +$("#lvDocuments").data("kendoListView").dataSource.view()[index].itemText;
link.click();