简体   繁体   English

如何使用JavaScript设置chrome扩展的文件下载位置?

[英]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. 嗨我正在使用chrome扩展名下载所选链接,但我无法设置下载位置。 All the urls downloaded to default location of chrome. 所有网址都下载到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. 我们可以从这里提示Chrome扩展弹出窗口中的目录选择器对话框用户可以选择下载路径。需要我方的任何信息让我知道。

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 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
                    });
}

Since you are generating and downloading just one ZIP file, you can use the chrome.downloads.download() method. 由于您只生成和下载一个ZIP文件,因此可以使用chrome.downloads.download()方法。 Eg: 例如:

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. 如果省略SaveAs对话框的显示,则只能指定位于用户定义的下载文件夹内或其子文件夹中的文件名。


Regarding the issue with the popup (see comment below): You should call the function from your background-page, not the popup. 关于弹出窗口的问题(请参阅下面的评论):您应该从后台页面调用该函数,而不是弹出窗口。 Eg you could use chrome.runtime.sendMessage / onMessage to pass a message to your background-page: 例如,您可以使用chrome.runtime.sendMessage / onMessage将消息传递到您的后台页面:

In background.js : background.js中

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

In popup.js : popup.js中

...
chrome.runtime.sendMessage({
    action: 'zipAndSave',
    params: ['url1', 'url2', 'url3']
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM