How to set the file download location for chrome extension Using JavaScript?

前端 未结 1 474
春和景丽
春和景丽 2021-01-01 03:19

Hi i am downloading selected links using chrome extension but I can\'t set downloads location. All the urls downloaded to default location of chrome. i know we can\'t do it

相关标签:
1条回答
  • 2021-01-01 04:13

    Since you are generating and downloading just one ZIP file, you can use the chrome.downloads.download() method. E.g.:

    var content = zip.generate();
    var zipName = 'download.zip';
    var dataURL = 'data:application/zip;base64,' + content;
    chrome.downloads.download({
        url:      dataURL,
        filename: zipName,
        saveAs:   true
    });
    count = 0;
    

    If you omit the display of a SaveAs dialog, then you can only specify a file name that is inside the user-defined download folder or in a subfolder of it.


    Regarding the issue with the popup (see comment below): You should call the function from your background-page, not the popup. E.g. you could use chrome.runtime.sendMessage/onMessage to pass a message to your background-page:

    In background.js:

    ...
    function zipAndSaveFiles(...) { ... }
    chrome.runtime.onMessage.addListener(function(msg, sender) {
        if ((msg.action === 'zipAndSave')
                && (msg.params !== undefined)) {
            zipAndSaveFiles(msg.params);
        }
    });
    

    In popup.js:

    ...
    chrome.runtime.sendMessage({
        action: 'zipAndSave',
        params: ['url1', 'url2', 'url3']
    });
    
    0 讨论(0)
提交回复
热议问题