Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 because of security reason. can we prompt directory chooser dialog in chrome extension popup from here user can select the Download path.Need any information from my side let me know.

Is this possible at all? Any suggestions on how to go about it?

Thanks in advance My code

function downloadFile(url, onSuccess,arrayOfUrl,zip) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = "blob";
    xhr.onreadystatechange = function () {

        if (xhr.readyState == 4) {
            if (onSuccess)
            {
            onDownloadComplete(xhr.response, arrayOfUrl,zip)
             }
}
}
xhr.send("null");
}
function onDownloadComplete(blobData,urls,zip ){
    if (count < urls.length) {
        blobToBase64(blobData, function(binaryData){
                var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
                 zip.file(fileName+".docx", binaryData, {base64: true}); 
                if (count < urls.length -1){
                    count++;
                    downloadFile(urls[count], onDownloadComplete, urls,zip);

                }
                else {

                    var content = zip.generate();

                     var zipName = 'download.zip';
                var a = document.createElement('a'); 
                a.href = "data:application/zip;base64," + content;
                a.download = zipName;
                a.click();
                  count = 0;

                }
            });
    }
}

popup.js

function onDownloadComplete(blobData,urls,zip ){


    if (count < urls.length) {
        blobToBase64(blobData, function(binaryData){
                // add downloaded file to zip:
                var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
               // zip.file(fileName, binaryData, {base64: true});
                 zip.file(fileName+".docx", binaryData, {base64: true}); //file"+count+".docx"
                if (count < urls.length -1){
                    count++;
                    downloadFile(urls[count], onDownloadComplete, urls,zip);

                }
                else {
                chrome.runtime.getBackgroundPage(function () {
            zipAndSaveFiles(zip);});



            }

            });
    }
}

**background.js**

function zipAndSaveFiles(zip)
{
    var content = zip.generate(zip);
                   var zipName = 'download.zip';
                   var dataURL = 'data:application/zip;base64,' + content;
                   chrome.downloads.download({
                   url:      dataURL,
                   filename: zipName,
                    saveAs:   true
                    });
}
like image 538
Arvind Anandala Avatar asked Oct 21 '22 17:10

Arvind Anandala


1 Answers

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']
});
like image 184
gkalpak Avatar answered Nov 01 '22 13:11

gkalpak