如何设置Chrome扩展的文件下载位置使用JavaScript? [英] How to set the file download location for chrome extension Using JavaScript?

查看:220
本文介绍了如何设置Chrome扩展的文件下载位置使用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用Chrome扩展程序下载选定的链接,但我无法设置下载位置。所有的网址都下载到Chrome的默认位置。我知道我们不能这样做是因为安全原因。我们可以在这里提示在Chrome扩展弹出窗口中的目录选择器对话框,用户可以选择下载路径。需要我的任何信息让我知道。

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


推荐答案

由于您正在生成和下载只需一个ZIP文件,您可以使用 chrome .downloads.download() 方法。例如:

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;






如果您省略显示 SaveAs 对话框,那么您只能指定位于用户定义的下载文件夹或其子文件夹内的文件名。


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.

关于弹出窗口的问题(请参阅下面的注释):
您应该从背景页面调用该函数,而不是弹出窗口。例如。您可以使用 chrome.runtime.sendMessage / onMessage code> 将消息传递到您的背景页面:

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:

background.js em>:

In background.js:

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

popup.js p>

In popup.js:

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

这篇关于如何设置Chrome扩展的文件下载位置使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆