一、文件下载
- excel文件下载
django接口内写法
file_path = os.path.join( settings.DOWNLOADS_DIR + "/" + file_name)
if os.path.exists(file_path):
response = FileResponse(open(file_path, 'rb'))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = f'attachment;filename="{download_url}"'
return response
vue中写法
安装
npm install xlsx
vue3的写法
import * as XLSX from 'xlsx'
保存方法
const ws = XLSX.utils.aoa_to_sheet(res.data);
const wb = XLSX.utils.book_new();
let fileName = 文件名称.xlsx
XLSX.utils.book_append_sheet(wb, ws, val.project_name);
XLSX.writeFile(wb, fileName);
- csv文件下载
django接口内写法
file_path = os.path.join( settings.DOWNLOADS_DIR + "/" + file_name)
if os.path.exists(file_path):
response = FileResponse(open(file_path, 'rb'))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = f'attachment;filename="{download_url}"'
return response
vue中写法
let fileContent = res;
let fileName = 文件名.csv
let blob = new Blob([fileContent]);
let downloadUrl = URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = downloadUrl;
a.download = fileName;
a.textContent = 'Download File';
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
- word文件下载–预留
- pdf文件下载–预留