iT邦幫忙

0

html <a href="" download> 下載連結問題

  • 分享至 

  • xImage

有沒有方法可以用 直接下載圖片

下邊是我的做法,這樣只會在另外一頁開啟,不能下載,我把href再做href="#" 再加上 data-href="連結" 下載的東西不是圖片,請問有方法解決嗎?

 <a href="https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg" target="_blank" download>Download</a>
canrong iT邦新手 3 級 ‧ 2022-06-20 16:10:24 檢舉
Browser Support...
https://www.w3schools.com/tags/att_a_download.asp
nick12345 iT邦新手 4 級 ‧ 2022-06-20 16:29:57 檢舉
想用連結直接下載
https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
純真的人
iT邦大師 1 級 ‧ 2022-06-20 15:55:44

恩?
HTML5以後版本都可以這樣下載@@a

參考
https://www.w3school.com.cn/tags/att_a_download.asp

<a href="https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg" download="圖片檔名.jpg" >Download</a>
nick12345 iT邦新手 4 級 ‧ 2022-06-20 16:28:37 檢舉

nick12345
你只能在你的網頁設定~不能在別人的網頁貼連結下載~
如果想在別人的網頁下載,要用滑鼠的右鍵→對連結按下後~開啟小選單,選另存連結下載~
https://ithelp.ithome.com.tw/upload/images/20220620/20061369vZRxG2jn7Z.png

nick12345 iT邦新手 4 級 ‧ 2022-06-20 17:11:31 檢舉

我需要按下BUTTON 就直接下載的方法,你的方法不是我想要的。

2
<input type="button" id="btn" value="Download" />
      <script>
            function download(filename, textInput) {

                  var element = document.createElement('a');
                  element.setAttribute('href','data:text/plain;charset=utf-8, ' + encodeURIComponent(textInput));
                  element.setAttribute('download', filename);
                  document.body.appendChild(element);
                  element.click();
                  //document.body.removeChild(element);
            }
            document.getElementById("btn")
                  .addEventListener("click", function () {
                        var text = document.getElementById("text").value;
                        var filename = "output.txt";
                        download(filename, text);
                  }, false);
      </script>

如果要在前端下載且不要用A連結的話。這需要的是JS來幫你處理。
當然了,其原理還是利用了一下A連結的Download屬性去做下載。
只是A連結是另外程式幫你建立出來的。

這邊只是幫你提個方式,請勿直接照抄。但相信你了解其原理的話。這修改起來不難的。

PS:我是不直接提供答案的人,請原諒。

3
貓虎皮
iT邦新手 3 級 ‧ 2022-06-20 19:49:48

下載連結:

<a href="https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg" download="23569861-sample-grunge-red-round-stamp.jpg">Download</a>

下載按鈕:

<a href="https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg" download="23569861-sample-grunge-red-round-stamp.jpg">
    <button>Download</button>
</a>

下載按鈕(想秀JavaScript版):

<button id="download23569861">Download</button>
<script>
(() => {
    let downloadButton = document.querySelector('#download23569861');
    downloadButton.addEventListener('click', () => {
        let a = document.createElement('a');
        a.href = 'https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg';
        a.download = '23569861-sample-grunge-red-round-stamp.jpg';
        // document.body.appendChild(a);
        a.click();
        // a.remove();
    });
})();
</script>

看到這裡,我知道上述的方法皆對您毫無幫助。
因為如果你將程式碼貼至html檔案內,點擊後,都只會以新分頁開啟圖片。
原因很簡單:瀏覽器因顧及安全性問題,而禁止跨域下載。
所以,您必須以img標籤做跨域圖片載入,並繪製到canvas後,再進行下載。
請看下方作法。

下載連結(跨域版):

<a href="javascript:download23569861();">Download</a>
<script>
    function download23569861(){
        let cvs = document.createElement('canvas'), 
            ctx = cvs.getContext('2d');
        // document.body.appendChild(cvs);
        let img = document.createElement('img');
        img.onload = () => {
            cvs.width = img.width;
            cvs.height = img.height;
            ctx.drawImage(img, 0, 0);
            var url = cvs.toDataURL();
            var a = document.createElement('a');
            a.href = url;
            a.download = '23569861-sample-grunge-red-round-stamp.jpg';
            a.click();
        };
        img.crossOrigin = 'anonymous';
        img.src = 'https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg';
    }
</script>

下載按鈕(跨域版):

<button onclick="download23569861();">Download</button>
<script>
    function download23569861(){
        let cvs = document.createElement('canvas'), 
            ctx = cvs.getContext('2d');
        // document.body.appendChild(cvs);
        let img = document.createElement('img');
        img.onload = () => {
            cvs.width = img.width;
            cvs.height = img.height;
            ctx.drawImage(img, 0, 0);
            var url = cvs.toDataURL();
            var a = document.createElement('a');
            a.href = url;
            a.download = '23569861-sample-grunge-red-round-stamp.jpg';
            a.click();
        };
        img.crossOrigin = 'anonymous';
        img.src = 'https://previews.123rf.com/images/aquir/aquir1311/aquir131100316/23569861-sample-grunge-red-round-stamp.jpg';
    }
</script>

我要發表回答

立即登入回答