nodejs 下载平台日志功能

node层代码:

@get('/DownloadExeLog') async DownloadExeLog() { const { requestId, context, } = this.ctx.request.query; //获取前台参数 const isLoad = true; try { await this.service.jobManage.getExeLog(requestId, context, isLoad); //调用后台接口 this.ctx.attachment('平台日志.txt');              //创建文件 this.ctx.set('Content-Type', 'application/octet-stream'); //设置请求头 this.ctx.body = fs.createReadStream('./exeLog.txt'); //将生成的文件,反给浏览器解析 } catch (err) { console.info('-----下载平台日志-----异常---:', err); this.ctx.body = { code: -1, msg: 'err', }; } fs.unlinkSync('./exeLog.txt'); //销毁本地服务生成的txt文件 }

getExeLog   请求后台的方法 :
public async getExeLog(requestId, context, isLoad) {
        let exeLog = await this.ctx.service.utilsService.proxyGen(this.ctx.proxy.jobManage.QueryExeLog, {
            request: {
                head: this.ctx.getCommonReqHead(),
                requestId,
                context,
                isLoad,
            },
            funcName: "jobManage.QueryExeLog",
        });
        fs.appendFileSync('exeLog.txt', exeLog.data.exeLog);   //将每次的请求结果,追加写入到txt文件中

        if (exeLog.data.context !== "") {
            await this.getExeLog(requestId, exeLog.data.context, isLoad);   //递归的形式,请求后台的接口(因为是分批上传)
        }
    }
<a href={ this.state.isPlantLog ? `/api/DownloadExeLog?requestId=${this.state.requestId}` : `/api/DownloadStageExe?exeId=${this.state.exeId}`}>   //用a标签去请求proto层,也就是node层的地址。因为ajax请求不会进行浏览器解析。 
   <Button className="log-btn">导出数据</Button>                                                                                                    //用a表情,返回的是txt格式,不会去渲染页面,会直接进行下载。 
</a>

 

posted @ 2020-06-11 16:51  清明|雨上  阅读(144)  评论(0)    收藏  举报