我有一个Web服务,该服务返回JSON对象,该对象包含应下载的文档字节数组。如何使用javascript下载该文档?
java代码
File file = new File(path);
bytesArray = new byte[(int) file.length()];
//read file into bytes[]
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
JSONObject response = new JSONObject();
response.put("document", bytesArray);
return response;
javascript代码
var blob = response.document;
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = "tst.pdf";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
我从javascript收到的响应是response = {"document",Object}
。