Django服务端读取excel文件并且传输到接口
path_name = "opboss_download_" + str(int(time.time())) + ".csv" print(path_name) write = Write_excel(path_name=path_name, sheet="opboss") data = ctx.get("data") for d in data: darr = [] darr.append(d.get("asrtime")) darr.append(d.get("app_id")) darr.append(d.get("app_name")) darr.append(d.get("asrdevicetype")) darr.append(d.get("asrdevicetypename")) darr.append(d.get("asrdeviceid")) darr.append(d.get("asr")) darr.append(d.get("tts")) darr.append(d.get("nlp")) darr.append(d.get("intent")) write.add_content(darr) write.write_now() def file_iterator(file_name, chunk_size=512): with open(file_name, 'rb') as f: while True: c = f.read(chunk_size) if c: yield c else: break response = StreamingHttpResponse(file_iterator(path_name)) response["Content-Type"] = "application/octet-stream" response["Content-Disposition"] = 'attachment;filename="{0}"'.format(path_name) return response
参考:https://www.jianshu.com/p/2ce715671340
前端vue处的实现方法:
<a href="javascript:;" class="btn" @click="getDownloadDataOrNot">点击下载</a>
// 限制数据导出条数,超出一万条范围不让导出 getDownloadDataOrNot(){ if (this.total > 10000){ alert("不好意思,你要导的数据超过1万条,请缩小导出范围,谢谢合作!") }else { this.getDownloadData(this.deviceid, this.page, this.size, this.startdate, this.enddate, this.app_id, this.intent, this.device_type_id); } },
大致是上面这样,如有错误,还请大虾们指教!