[英]Force download image from external server with Jquery
我想用javascript / jquery從外部服務器(我不擁有)下載圖像。
用我的實際腳本unluckely我只能在瀏覽器中打開img。
腳本感興趣
var aa = $('<a/>', {"href": img ,"text": "Download", "download": ""}).appendTo( "#show" );
有類似的問題,但沒有jquery / javascript。 我不想重命名文件^^
一個簡單的解決方案是在鏈接中寫入“右鍵單擊並保存鏈接為”,但不是很好:D
注意:我從這個答案中得到了幫助
我想你想在某些事件上下載圖像,例如Click Event等。
根據我的理解,我認為應該點擊圖片或點擊下載所有圖片
您可以在圖像元素中添加一個類來繪制圖像
<img class="downloadable" ...>
$('img.downloadable').on('click', function(){
var $this = $(this);
$this.wrap('<a href="' + $this.attr('src') + '" download />')
});
或者如果您想要點擊下載所有圖像,那么您可以嘗試此代碼
<button id="download_all_images">Download All Images</button>
$('#download_all_images').on('click', function(){
$('img.downloadable').each(function(){
var $this = $(this);
$this.wrap('<a href="' + $this.attr('src') + '" download />')
});
});
由於HTML5下載屬性,此代碼段將強制下載您的圖像
您可以檢查此代碼以驗證它我是從外部服務器下載圖像
<button id="download_image">Download Image</button>
$('#download_image').click();
<a id="download_image" href="https://docs.google.com/uc?id=0B0jH18Lft7ypSmRjdWg1c082Y2M" download></a>
我希望這個能幫上忙! 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.