javascript 圖片下載

情景:

oss上面存儲的圖片,如果設(shè)置了 Content-Disposition 為 attachment,那么訪問圖片的時候是直接下載的;否則,訪問是預(yù)覽形式;

解決:
  1. 到oss客戶端設(shè)置Content-Disposition,
  2. 后端設(shè)置 Content-Disposition,
  3. 前端實現(xiàn),代碼如下:
疑問:
  1. 設(shè)置了download屬性為什么還要blobUrl?【有知道的幫忙解答下】
  2. URL.createObjectURL和URL.revokeObjectURL是干什么的?【最后有解答】
  3. 生成的alink.href為什么不能在地址欄輸入直接訪問?【最后有解答】
  4. ie瀏覽器 navigator.msSaveBlob 是干什么的?【最后有解答】
  5. 為什么要appendChild 和 remove ?【最后有解答】
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let url  = "http://mybg.oss-cn-hangzhou.aliyuncs.com/2019031510452423"
        const fileName = '2019031510452423.png'
        let xhr = new XMLHttpRequest();
        xhr.open("get", url, true); // true 表示異步
        xhr.responseType = "blob";
        xhr.onload = function (res) {
            if (this.status == 200) {
                var blob = this.response;
                if ('download' in document.createElement('a')) { // 非IE下載
                    const alink = document.createElement('a')
                    alink.download = fileName
                    alink.style.display = 'none'
                    alink.href = URL.createObjectURL(blob) // 將blob數(shù)據(jù)轉(zhuǎn)換成BlobUrl
                    document.body.appendChild(alink)
                    alink.click()
                    URL.revokeObjectURL(alink.href) // 釋放URL 對象
                    document.body.removeChild(alink)
                } else { // IE10+下載
                    navigator.msSaveBlob(blob, fileName)
                }
            }
        }
        xhr.send();
    </script>
</body>

</html>
URL.createObjectURL和URL.revokeObjectURL是干什么的?

URL.createObjectURL() 靜態(tài)方法會創(chuàng)建一個 DOMString,其中包含一個表示參數(shù)中給出的對象的URL。這個新的URL 對象表示指定的 File 對象或 Blob 對象。

在每次調(diào)用 createObjectURL() 方法時,都會創(chuàng)建一個新的 URL 對象,即使你已經(jīng)用相同的對象作為參數(shù)創(chuàng)建過。當(dāng)不再需要這些 URL 對象時,每個對象必須通過調(diào)用 URL.revokeObjectURL() 方法來釋放。
瀏覽器在 document 卸載的時候,會自動釋放它們,但是為了獲得最佳性能和內(nèi)存使用狀況,你應(yīng)該在安全的時機主動釋放掉它們。

生成的alink.href為什么不能在地址欄輸入直接訪問?

這個 URL 的生命周期和創(chuàng)建它的窗口中的 document 綁定。

ie瀏覽器 navigator.msSaveBlob 是干什么的?

存儲blob或file的方法,非標(biāo)準(zhǔn),不推薦,僅出于兼容目的保留

The Navigator.msSaveBlob() method saves the File or Blob to disk.

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

Deprecated
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

為什么要appendChild 和 remove ?

代碼中對創(chuàng)建的<a> 進行的 appendChild 和 remove 操作主要是為了兼容 FireFox 瀏覽器,在 FireFox 瀏覽器下調(diào)用該方法如果不將創(chuàng)建的<a>標(biāo)簽添加到 body 里,點擊鏈接不會有任何反應(yīng),無法觸發(fā)下載,而在 Chrome 瀏覽器中則不受此影響。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 夜鶯2517閱讀 128,087評論 1 9
  • 版本:ios 1.2.1 亮點: 1.app角標(biāo)可以實時更新天氣溫度或選擇空氣質(zhì)量,建議處女座就不要選了,不然老想...
    我就是沉沉閱讀 7,361評論 1 6
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,814評論 28 54
  • 兔子雖然是枚小碩 但學(xué)校的碩士四人寢不夠 就被分到了博士樓里 兩人一間 在學(xué)校的最西邊 靠山 兔子的室友身體不好 ...
    待業(yè)的兔子閱讀 2,759評論 2 9

友情鏈接更多精彩內(nèi)容