2017-02-28 67 views
-2

此下載一張圖片的網址:http://www.clipartbest.com/cliparts/Kij/gGE/KijgGEgrT.png通過URL

這是我的網頁代碼:

<html> 
<head><title>Some title</title> 
<script> 
function DownloadUrl(url) 
{ 
// Download that url 
} 
</script> 
<body> 
<button onclick="DownloadUrl('http://www.clipartbest.com/cliparts/Kij/gGE/KijgGEgrT.png')">button</button> 
</body> 
</html> 

我的網頁和圖片的URL頁面是不同的。

+0

下載attibute https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download – epascarello

+0

是什麼你在這裏問? – user3378165

+0

@ user3378165感謝您的回覆,請閱讀標題 – techno01

回答

-1

與此

<script> 
function DownloadUrl(url) 
{ 
window.location.href = url; 
} 
</script> 
+0

@ Shobhit WaliaThanks回覆,你的代碼在新標籤打開網址,但我想下載該網址 – techno01

+0

我認爲你需要編寫服務器端代碼下載圖像

+0

它一直顯示在新頁面上的鏈接 – techno01

0

你並不需要JavaScript來從URL下載一個文件試試,你可以使用一個<a>標籤,並添加download HTML屬性如下:

<html> 
<head><title>Some title</title> 
<body> 
<a href="http://www.clipartbest.com/cliparts/Kij/gGE/KijgGEgrT.png" download> Download here! </a> 
</body> 
</html> 

這是最常見的解決方案。

你也可以這樣來做:

<form method="get" action="http://www.clipartbest.com/cliparts/Kij/gGE/KijgGEgrT.png"> 
<button type="submit">Download!</button> 
</form> 
+0

我上面說過「PS:我的頁面和圖片的url頁面有點不同」,意思是我想從其他網站下載 – techno01

+0

你可以看看[這裏](https ://www.w3schools.com/tags/tryit.asp?filename = tryhtml5_a_download)並查看html'download'屬性如何正是您所需要的。 – user3378165

+0

你的mothode只能用於一個網址如果我想下載5,我想要什麼動態的,我認爲它通過一些javascript功能完成 – techno01