kindring 3 years ago
parent
commit
1ee491c8a5
1 changed files with 25 additions and 0 deletions
  1. 25 0
      js/下载功能.js

+ 25 - 0
js/下载功能.js

@@ -0,0 +1,25 @@
+/*
+ * @Description: 
+ * @Autor: kindring
+ * @Date: 2021-10-12 09:48:05
+ * @LastEditors: kindring
+ * @LastEditTime: 2021-10-12 09:48:05
+ * @LastDescript: 
+ */
+var blob = res.data;
+// FileReader主要用于将文件内容读入内存
+var reader = new FileReader();
+reader.readAsDataURL(blob);
+// onload当读取操作成功完成时调用
+reader.onload = function(e) {
+    var a = document.createElement('a');
+    // 获取文件名fileName
+    var fileName = res.headers["content-disposition"].split("=");
+    fileName = fileName[fileName.length - 1];
+    fileName = fileName.replace(/"/g, "");
+    a.download = fileName;
+    a.href = e.target.result;
+    document.body.appendChild(a);
+    a.click();
+    document.body.removeChild(a);
+}