js 接收后台二进制流转成文件格式

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);






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值