2014-06-04 27 views
0

我試圖以helloWorld.png的形式下載文件,但它總是保存爲download.png。Javascript:如何使用特定的文件名保存圖片而不是download.png

你能幫我確定我錯過了什麼嗎?

var $container = $('#svg-container'), 
     // Canvg requires trimmed content 
    content = $container.html().trim(), 
    canvas = document.getElementById('thecanvas'); 
    // Draw svg on canvas 
    canvg(canvas, content); 
    // Change img be SVG representation 
    var theImage = canvas.toDataURL('image/png'); 
    $('#svg-img').attr('src', theImage); 



function downloadWithName(uri, name) { 

    function eventFire(el, etype){ 
     if (el.fireEvent) { 
      (el.fireEvent('on' + etype)); 
     } else { 
      var evObj = document.createEvent('Events'); 
      evObj.initEvent(etype, true, false); 
      el.dispatchEvent(evObj); 
     } 
    } 

    var link = document.createElement("a"); 
    link.download = name; 
    link.href = uri; 
    eventFire(link, "click"); 

} 

downloadWithName(theImage, "helloWorld.png") 

http://jsfiddle.net/shanthisivanesan/a2FLx/22/

+0

你認爲html5 download =「filename.ext」 –

+0

我必須做這個programaticaly,就像我在jsfiddle例子中動態創建錨標籤一樣。你能讓我知道我錯過了什麼嗎? – user3646404

+0

似乎直接與base64一起工作 - > ** http://jsfiddle.net/W48wQ/**,所以我猜測它與canvg()所做的任何操作以及base64的檢索方式有關。你等待畫布加載等? – adeneo

回答

0

試試這個jQuery代碼,它適用於最新的火狐(其中URI是datauri和在圖像文件名)

$('<a>',{ 
    type: "submit", 
    download: name, 
    href: uri, 
    id: 'download_link' 
}).appendTo('body'); 


$('a#download_link')[0].click(); 
相關問題