js受信バックグラウンドバイナリはファイル形式になります.
2603 ワード
javaバックグラウンド:
@RequestMapping(value = "/audit", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public void audit(@RequestParam("id") long id, @RequestParam("status") String status,HttpServletResponse response) {
JsonObject jsonObject = new JsonObject();
try {
String redeemCodeString="";
response.reset();
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename="+"redeemCodeString.txt");
outputStream.write(redeemCodeString.getBytes(),0,redeemCodeString.getBytes().length);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
jsonObject.addProperty("msg"," !");
jsonObject.addProperty("code",1);
}
}
js: var xhr = new XMLHttpRequest();
var str = "id=" + ticketId + "&status=" + status;
xhr.open('POST', "/admin/audit", true); // POST ,
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.responseType = "blob"; // blob
xhr.onload = function () {
//
if (this.status === 200) {
var blob = this.response;
if(blob.size>0){
var reader = new FileReader();
reader.readAsDataURL(blob); // base64, a href
reader.onload = function (e) {
// , a
var a = document.createElement('a');
a.download = ' .txt';
a.href = e.target.result;
$("body").append(a); // firefox click
a.click();
$(a).remove();
window.location.reload();
}
}else{
window.location.reload();
}
}
};
xhr.send(str);