javascript - 使用 django 和纯 javascript 提供现有的 excel 文件
问题描述
我正在尝试使用 django 提供现有的 excel 文件,使用 javascript 下载它。
我在 django 视图中尝试了以下代码:
Generate_Report is the io.Bytes() read from xlsxwriter.
response=HttpResponse(Generate_Report.output,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition']='attachment;filename=Report.xlsx'
return response
在html中我写了这个
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200) {
var downloadLink = window.document.createElement('a');
var contentTypeHeader = xhttp.getResponseHeader("Content-Type");
downloadLink.href = window.URL.createObjectURL(new Blob([xhttp.response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}));
downloadLink.download = "Report.xlsx";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
};
我想以下载格式提供现有的 excel 文件。
解决方案
推荐阅读
- javascript - CallKit CXCallAction 事务挂起
- flutter - 如何从 Flutter 插件调用代码?
- pandas - 如何将 Pandas 数据框直接上传到 BigQuery?
- apt - deb包解压到文件夹时如何安装
- rxjs - RxJS 从 mergeMap 返回值数组
- javascript - React 传单:Array.map 函数未将 Marker 组件渲染到 Map
- docker - Docker 网络,从 windows 主机连接到 linux 容器
- javascript - JQuery datepicker验证问题
- javascript - 当 document.hidden 为 true 时,Chrome 中不会触发滚动事件。任何解决方法?
- python - 通过创建字典替换单词