下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

web前端:http请求post,文件导出兼容IE10+

作者:Jade_g     来源: https://www.cnblogs.com/gxp69/p/11283080.html点击数:880发布时间: 2020-02-28 11:23:41

标签: httpweb前端超文本传输协议

Web开发

  超文本传输协议(HTTP,HyperTextTransferProtocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收html页面的方法。

  1.post的方法里要加responseType:'blob'参数,不然下载的Excel会乱码

  2.使用{type:"application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx

  3.返回结果为下载excel文档链接,使用window.open(result)即可

  4.使用增加节点调用click方法,而不使用window.open(objectUrl)方法,是防止被浏览器当插件屏蔽弹出连接

  5.给文件设定名字,直接在a标签的download属性中设置即可

  1、方法1

  axiOS.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{

  letreader=newFileReader()

  reader.readAsDataURL(res);

  reader.onload=(e:any)=>{

  vara=document.createElement('a');

  document.body.appendChild(a);

  a.style.display='none';

  a.href=e.target.result;

  a.download='name.xlsx';

  a.click();

  a.remove();

  }

  })

  2、方法2

  vardownload=function(file_name:string,content:any){

  varcsvData=newBlob([content],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});

  //forIE

  if(window.navigator&&window.navigator.msSaveOrOpenBlob){

  window.navigator.msSaveOrOpenBlob(csvData,file_name);

  }

  //forNon-IE(chrome,firefoxetc.)

  else{

  vara=document.createElement('a');

  document.body.appendChild(a);

  a.style.display='none';

  varurl=window.URL.createObjectURL(csvData);

  a.href=url;

  a.download=file_name;

  a.click();

  a.remove();

  window.URL.revokeObjectURL(url);

  }

  };

  this.$axios.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{

  download('name',res);

  })

  TedNelson组织协调万维网协会(WorldWideWebConsortium)和互联网工程工作小组(InternetEngineeringTaskForce)共同合作研究,最终发布了一系列的RFC,其中著名的RFC2616定义了HTTP1.1。

赞(11)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程