1.导出为zip或excel表格,以下以导出excel表格为例

image.png
image.png
image.png

  1. /**service.httpDownload
  2. *请求处理
  3. */
  4. httpDownload (options, type) {
  5. type = type || 'post';
  6. let params = options.data;
  7. if(options && options.data) {
  8. // 将传递的参数进行去空格的处理
  9. if (typeof params == 'object') { // 判断params是否为对象类型
  10. for (let key in params) {
  11. if (typeof params[key] == 'string') { // 判断属性值是否是string类型
  12. params[key] = params[key].trim();
  13. }
  14. }
  15. }
  16. }
  17. return new Promise(function (resolve, reject) {
  18. Axios(
  19. {
  20. headers: {
  21. token: getToken() || ''
  22. },
  23. method: type,
  24. url:options.url,
  25. data: params,
  26. responseType: 'blob' // 表明返回服务器返回的数据类型
  27. }).then(res => {
  28. resolve(res)
  29. }).catch(error => {
  30. reject(error);
  31. })
  32. })
  33. },
  34. /**this.$api.uniformDirectMake.downLoadWords
  35. *接口api
  36. */
  37. // 下载应急文字(excel导出)
  38. downLoadWords: (params) => {
  39. const options = {
  40. url: HttpPrefix.COMMAND + '/command/ecmdCommand/exportCommand',
  41. data: params,
  42. message: 'Y'
  43. }
  44. return service.httpDownload(options, 'post').then(resp => {
  45. return resp;
  46. });
  47. },
  48. /**utils.downloadFile
  49. * 将二进制流用a标签的方式下载文件
  50. * data: 要下载的文件流
  51. * fileName: 下载时保存的文件名称+文件类型
  52. */
  53. downloadFile (data, fileName) {
  54. if (!data || !fileName) {
  55. Message.error('下载的文件流或保存的文件名称为空,请修改!');
  56. return;
  57. }
  58. // let blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;'});
  59. if(window.navigator.msSaveBlob){ //兼容IE
  60. let blob = new Blob([data], {type: "application/vnd.ms-excel"});
  61. window.navigator.msSaveBlob(blob,fileName)
  62. }else{
  63. const url = window.URL.createObjectURL(new Blob([data]));
  64. const link = document.createElement('a');
  65. link.style.display = 'none';
  66. link.href = url;
  67. link.setAttribute('download',fileName);
  68. document.body.appendChild(link);
  69. link.click();
  70. }
  71. },
  72. // 下载应急文字(excel导出)
  73. downLoadWords(row){
  74. let params = {
  75. examPlanId: this.formData.examPlanId,
  76. }
  77. this.downloadingworlds = true;
  78. this.$api.uniformDirectMake.downLoadWords(params).then(res =>{
  79. if(res.status=='300'){
  80. let _this = this;
  81. let reader = new FileReader();
  82. reader.onload = function(event) { // reader.onload:文件读取成功完成时触发
  83. let content = reader.result;
  84. try {
  85. let contentObj = JSON.parse(content);
  86. if (contentObj != null && contentObj.error != null) {
  87. _this.$message.error(contentObj.error);
  88. } else {
  89. throw new Error('导出失败');
  90. }
  91. } catch(e) {
  92. _this.$message.error('导出失败', e);
  93. } finally {
  94. _this.downloadingworlds = false;
  95. }
  96. }
  97. reader.readAsText(res.data);
  98. }else {
  99. utils.downloadFile(res,'指令应急文字导出.xlsx');
  100. this.downloadingworlds = false;
  101. }
  102. }).catch(errot =>{
  103. this.downloadingworlds = false;
  104. this.$message.error('导出失败',error);
  105. })
  106. },

2.下载文件

image.png