XMLHttpRequest下载文件,js下载文件,支持后台消息在前端页面提示
Posted 棉晗榜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XMLHttpRequest下载文件,js下载文件,支持后台消息在前端页面提示相关的知识,希望对你有一定的参考价值。
js
//获取token
var token = sessionStorage.getItem('token');
var url = "/article/ExportEXCEL_PageOfOrderList?id=12312131&verifyKey=kkm";
var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.responseType="blob";
xhr.setRequestHeader('token', token);
xhr.onload = function (e)
console.log(772, xhr);
var contentType = xhr.getResponseHeader("content-type");
console.log(779, contentType);
if (contentType.indexOf("text/") != -1)
//后台消息提示
//将blob转换为文本
var utf8decoder = new TextDecoder();
var fr = new FileReader();
fr.readAsArrayBuffer(this.response);
fr.onload = function(res)
var buffer = fr.result;
var text = utf8decoder.decode(buffer);//String
alert(text);
return;
var blob = new Blob([this.response], type: contentType );
if (this.status == 200)
var filenameHead = xhr.getResponseHeader("Content-disposition");
console.log('filename=' + filenameHead);
var urlArr = filenameHead.split(';')[1];
console.log(urlArr);
var filename = urlArr.split('=')[1];
console.log('filename=' + filename);
//var blob = this.response;
var a = document.createElement('a');
var urlTemp = URL.createObjectURL(blob);
a.href = urlTemp;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(urlTemp);
;
xhr.send();
后台导出excel文件
[TokenFilter]
[HttpGet("ExportEXCEL_PageOfOrderList")]
public async Task<ActionResult> ExportEXCEL_PageOfOrderList([FromQuery] SelectWhere selectWhereStr)
var result = await mq_DataDisposeBusiness.GetPageOfOrderList(selectWhereStr, 1, 40000);
if (result.Code != ResultEnum.Success)
if (result.ErrorMessage.Equals("未找到数据"))
return Content("导出内容不能为空", "text/html;charset=UTF-8");
return Content(result.ErrorMessage, "text/html;charset=UTF-8");
if (result.Payload.Total == 0)
return Content("导出内容不能为空", "text/html;charset=UTF-8");
//生成EXCEL文件
List<List<object>> exportData = new List<List<object>>();
string columName = "条码号,样本类型,检测时间,科室名称,智能分类建议,溶血(mol/L),黄疸(mol/L),乳糜(mol/L),用户反馈";
foreach (var item in result.Payload.List)
var row = new List<object>();
row.Add(item.OrderId);
row.Add(item.OrderTypeName ?? "其它");//样本类型
string time = "";
if (item.OrderCreateTime.HasValue)
time = item.OrderCreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
row.Add(time);//检测时间
row.Add(item.Department);//科室名称
row.Add(item.ResultStr);//智能分类建议
row.Add(item.HNum.ToString() == "0" ? "--" : item.HNum.ToString());//溶血(mol/L)
row.Add(item.INum.ToString() == "0" ? "--" : item.INum.ToString());//黄疸(mol/L)
row.Add(item.LNum.ToString() == "0" ? "--" : item.LNum.ToString());//乳糜(mol/L)
row.Add(item.UserLable);//用户反馈
exportData.Add(row);
string fileName = "样本查询" + DateTime.Now.Ticks + ".csv";
string downUrl = $"/ CommonNameValueConfig.ExportDir/" + fileName;
string path = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.Combine(CommonNameValueConfig.ExportDir, fileName);
WebAPI_Serum.Model.CsvHelper.ExportMakeCsv(exportData, path, "样本导出", columName);
return PhysicalFile(path, "application/octet-stream", fileName);
以上是关于XMLHttpRequest下载文件,js下载文件,支持后台消息在前端页面提示的主要内容,如果未能解决你的问题,请参考以下文章