How to force Chrome to NOT open SaveAs Dialog when downloading a URL?

10,008

Solution 1

Edit : I've added complete sample code for multiple file downloads which doesn't show SaveAs Dialog.

You can achieve this by using chrome.downloads API.

manifest.json

{
  "description": "Multiple file downloads without showing SaveAs Dialog",
  "background": {
     "scripts": [ "background.js" ],
     "persistent" : true
  },
  "content_scripts": [{
     "js": [ "content_script.js"],
     "matches": [ "<all_urls>" ],
     "run_at": "document_start"
  }],
  "manifest_version": 2,
  "name": "MultipleFileDownloads",
  "permissions": [ "downloads" ],
  "short_name": "MFD",
  "version": "0.0.0.1"
}

content_script.js

var DOWNLOAD_LIMIT = 100;

function downloadURL(url, filename, callback){
    chrome.runtime.sendMessage({
        download_url : url,
        filename : filename
    },function(){
        if(typeof callback == 'function'){
            callback();
        }
    })
}

function simulateFileDownload(i){
    if(i > DOWNLOAD_LIMIT){
        document.getElementById('download_btn').disabled = false;
        return false;
    }
    var blob = new Blob(['This is sample file '+i], {type:'text/plain'});
    var url = URL.createObjectURL(blob);
    downloadURL(url,'Sample-'+i+'.txt',function(){
        URL.revokeObjectURL(url);
        i++;
        simulateFileDownload(i);
    })
}

window.onload = function(){
    var btn = document.createElement('button');
    btn.id = 'download_btn';
    btn.style.cssText = 'position:fixed;top:10px;left:10px;width:140px;height:30px;z-index:1000000;';
    btn.textContent = 'Download Files';
    document.body.appendChild(btn);
    btn.addEventListener('click',function(){
        this.disabled = true;
        simulateFileDownload(0);
    })
}

background.js

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
    if(message.download_url){
        chrome.downloads.download({
            url : message.download_url,
            filename : message.filename,
            saveAs : false
        })
    }
});

Solution 2

It is impossible when "Ask where to save each file before downloading" Enabled (as of 70.0.3538.77). The corresponding Chromium bug is:

Bug 417112: chrome.downloads.download ignore saveAs

Moreover setting filename in chrome.downloads.downloads() also doesn't work.

Bug 758094: Extension can not rename downloaded file

Share:
10,008
QuteBits
Author by

QuteBits

Updated on July 21, 2022

Comments

  • QuteBits
    QuteBits almost 2 years

    Chrome Build: the newest, 33+

    A Chrome Extension extracts certain urls from currently viewed site and then downloads a subset of them (quite often hundreds of files).

    Expected Behavior:

    Files are downloaded into the default Download-Folder without questioning where and under which filename they have to be saved.

    Problem:

    If a user has enabled the option "Ask where to save each file before downloading" in Chrome->Settings->Advanced Settings->Downloads then when trying to download, for example, 100 files simultaniously, Chrome tries to open 100 SaveAs Dialogs and crashes.

    What I tried:

    • to use chrome.downloads.download(object options, function callback) method with an option saveAs: false
    • using the following code to trigger a download through an emulated mousevent:

      function saveAs(Url,filename){
        var blob=new Blob([''], {type:'application/octet-stream'}); 
        var url = webkitURL.createObjectURL(blob);
        var a = document.createElementNS('http://www.w3.org/1999/xhtml','a');
        a.href = Url;
        a.download = filename; 
        var e = document.createEvent('MouseEvents');
        e.initMouseEvent('click', false, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        a.dispatchEvent(e);
        webkitURL.revokeObjectURL(url);
      }
      
  • Raju Singh
    Raju Singh over 8 years
    Can you share your full example. I am not able to install your shared crx.
  • Jitendra singh
    Jitendra singh over 8 years
    Hi can you please share the source code. I have to implement download feature in chrome app.
  • thdoan
    thdoan almost 8 years
    In Chrome v53 on Windows 7 the Save As dialog is always displayed for me, even when using this example. By the way, you have a typo: in background.js message.url should be message.download_url .