导出:Excel导入导出
导出:
1. 后端导出,返回一个excel文件
1). 前端通过 <a href="exportUrl" /> 来下载
2). window.open(fileUrl)
2. 前端导出
可以使用js-xlsx: https://github.com/SheetJS/js-xlsx
3. 后端返回二进制文件流 前端通过Blob对象生成文件
设置http请求的responseType为blob
let blob = new Blob([res.result], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename + moment().format("YYYYMMDDHHmmss") + ".xlsx";
document.body.appendChild(link);
link.click();
link.remove();
4. 异步导出 前端点击导出,开始导出任务,后端执行导出 直到后端完成,前端显示可下载
导入/上传:
1. 使用上传组件(jquery upload 组件/vue el-upload/iview Upload)
2. 手动发送二进制数据:
使用FormData对象,
1. 设置http请求头: 'Content-Type': 'multipart/form-data'
2. 创建FormData
let fd = new FormData()
fd.append('file', file)
this.loadingText = '上传中...'
commonService.uploadImg(fd).then()
(https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#interface-formdata)