About js file download rename problem

Let me talk about the situation of problems, the first is the time to add back-end file upload will file a timestamp, the other because of the different file server addresses an issue that causes cross-domain, so download a label = "filename" does not take effect

Reference https://www.jianshu.com/p/6545015017c4

solution:

Without cross-domain 1

  <a href="url" download="filename" />

  But pay attention to download the compatibility properties, refer to w3c document content

solution:

2: cross-domain case

1   / * 
2          * Get BLOB
 . 3          * @params {String} URL address file
 . 4          * @ the params} {{String} name rename filename
 . 5          * @return {} Promise
 . 6          * / 
. 7          downloads (URL, filename) {
 . 8              the this .getBlob (URL) .then (BLOB => {
 . 9                  the this .SaveAs (BLOB, filename)
 10              })
 . 11          },
 12 is          getBlob (URL) {
 13 is              return  new new Promise (Resolve => {
 14                  const XHR = new new the XMLHttpRequest ()
15                 xhr.open('GET', url, true)
16                 xhr.responseType = 'blob'
17                 xhr.onload = () => {
18                     if (xhr.status === 200) {
19                         resolve(xhr.response)
20                     }
21                 }
22                 xhr.send()
23             })
24         },
25         saveAs (blob, name) {
26             const link = document.createElement('a')
27             link.href = window.URL.createObjectURL(blob)
28             link.download = name
29             link.click()
30         },

 

Guess you like

Origin www.cnblogs.com/lsh1011/p/11865446.html