2017-03-13 99 views
0
<img id='imgt' src='01.jpg' alt='img'> 
<input type='text' id='inputnewimg'> 

我填充輸入一些IMG URL,例如 - https://i.ytimg.com/vi/QX4j_zHAlw8/maxresdefault.jpg如何獲得下載圖片的完整路徑?

JS

$('#inputnewimg').keypress(function(e) { 
if (event.keyCode === 13) { 
    e.preventDefault(); 
    var abc = $(this).val(); 
    var dl = document.createElement("a"); 
    dl.href = abc; 
    dl.download = true; 
    document.body.appendChild(dl); 
    dl.click(); 
} 
}); 

上面的代碼下載圖像到默認的本地文件夾(下載)。
有沒有辦法來dinamically設置另一個目標文件夾,並獲取圖像路徑和名稱,因爲我想新的img自動成爲#imgt的來源?

+0

我不這麼認爲。至少不在現成的網絡瀏覽器中。 –

+0

@KevinB,謝謝。你認爲有可能通過將圖像轉換爲base64並且...我不確定...或使用畫布克隆圖像,而無需下載? – bonaca

回答

2

不,你不能遺憾。

您可以做的唯一的事情就是在下載之前更改文件名。

// You can use the code below to match the filename 
 
var srcOfImage = "http://mywebsite.com/images/beautiful_logo.png?even=withParamaters"; 
 
var filename = srcOfImage.match(/([\w\d_-]*)\.?[^\\\/]*$/i)[1]; 
 

 
// And then you can simply rename it with a replace 
 
srcOfImage = srcOfImage.replace(filename, "even_better_logo"); 
 

 
console.log(srcOfImage); 
 

 
// --> This way you keep your file extension 
 
// You could also simply add a prefix or suffix on the filename

+0

謝謝。你認爲有可能通過將圖像轉換爲base64並且...我不確定...或使用畫布克隆圖像,而無需下載? – bonaca

+0

我不確定,但你也可以爲此做一個Flash插件。這是什麼目的?也許有一個文件對話框的GitHub庫,你可以嘗試這樣做。 – Maxime2400

+0

我們的目標是在網上獲得任何img作爲'#imgt'的源代碼,而不僅僅是鏈接url,而是下載它並獲取它的'local url',並且一次將它作爲'#imgt'的源代碼,自動,一次點擊。 – bonaca

相關問題