Browse Source

download pic

begmoon 1 year ago
parent
commit
4a6463e31f
1 changed files with 22 additions and 28 deletions
  1. 22 28
      src/views/data/index.vue

+ 22 - 28
src/views/data/index.vue

@@ -467,38 +467,32 @@ export default {
 
             this.$api.getDataPic(this.queryParams).then((res)=>{
                 this.loading = false;
-                console.log(res.data)
-                var elink = document.createElement('a');
-                let blob=new Blob([res.data], {type: 'image/png'});
-                let objUrl=URL.createObjectURL(blob);
-                let file_name= decodeURIComponent(res.headers['content-disposition'].split('=')[1]);
-                console.log(file_name)
-                
-                elink.download = "12.png";// this.queryParams.stock_date + gname;
-                elink.style.display = 'none';                
-                elink.href = objUrl;
-                document.body.appendChild(elink);
-                elink.click();
-                document.body.removeChild(elink);  
+                this.getBlob(res.data.data.image_url).then(blob => {
+                    this.savePic(blob,res.data.data.filename)
+                })
                 
             })
         },
         
-        ways(blob, suffix,res) {
-            let elink = document.createElement("a");   // 创建一个<a>标签
-            elink.style.display = "none";                       // 隐藏标签
-            elink.href = window.URL.createObjectURL(blob);      // 配置href
-            // 获取后端返回的响应头中的名称
-            // let filename = res.headers["content-disposition"]; 
-            // let newFilename = filename.split(';')[1].split('=')[1];
-            //自定义名称
-            // let newFilename = "样例文件" + new Date().getTime() + suffix; //自定义名字
-            // let newFilename = decodeURIComponent(res.headers["content-disposition"].split(';')[1].split('=')[1])
-            // newFilename = decodeURIComponent('11.png');
-            elink.download = '11.png';
-            elink.click();
-            URL.revokeObjectURL(elink.href);   // 释放URL 对象(弹出框进行下载)
-            document.body.removeChild(elink);  // 移除<a>标签
+        getBlob(url) {
+            return new Promise(resolve => {
+                const xhr = new XMLHttpRequest()
+                xhr.open('GET', url, true)
+                xhr.responseType = 'blob'
+                xhr.onload = () => {
+                    if (xhr.status === 200) {
+                        resolve(xhr.response)
+                        }
+                    }
+
+                xhr.send()
+            })
+        },
+        savePic(blob, filename) {
+            var link = document.createElement('a')
+            link.href = window.URL.createObjectURL(blob)
+            link.download = filename
+            link.click()
         },
 
         del(row) {