上传
<input type="file" ref="inputFile" accept=".xls,.xlsx" @change="readFile($event)" style="display: none" />
js
//上传 async readFile(e) { const files = e.target.files if (files.length <= 0) { return } else if (!/(xls|xlsx)$/.test(files[0].name.toLowerCase())) { this.$message.warning('上传格式不正确,请上传xls、xlsx格式') } const form = new FormData() form.append('risenUpload', files[0]) let result = await demoUpload(form) if (result.success) { this.$message.success('上传成功') } else { this.$message.error(result.actionErrors[0]) } },
上传文件需要添加
headers: { 'Content-Type': 'multipart/form-data' },
下载
1. window.open 方法
window.open(接口地址, '_self')
2. 创建a标签点击 下载 切记 接口 配置添加 responseType
export function downDemoTemplate(params) {
return request({
url: '/downDemoTemplate',
method: 'get',
responseType: 'blob',
params
})
}
downDemoTemplate().then(res => { let blob = new Blob([res]) let fileName = '范例.xls' if (typeof window.navigator.msSaveBlob !== 'undefined') { window.navigator.msSaveBlob(blob, fileName) } else { let URL = window.URL || window.webkitURL let objectUrl = URL.createObjectURL(blob) if (fileName) { var a = document.createElement('a') if (typeof a.download === 'undefined') { window.location = objectUrl } else { a.href = objectUrl a.download = fileName document.body.appendChild(a) a.click() a.remove() } } else { window.location = objectUrl } } })