从浏览器通过Dropbox API下载文件

时间:2015-07-18 10:13:12

标签: javascript google-chrome-extension dropbox

我正在编写一个简单的Chrome扩展程序,我需要将文件上传并下载到Dropbox。正如Dropbox File Download API中所提到的,当我通过ajax请求下载文件时,我获得了可以在浏览器控制台中登录的文件内容。据我所知,从浏览器内的javascript中将内容写入磁盘是不可能/不可行的,所以我怎样才能这样做,以便用户可以download该文件。

修改

//Simple function to download a file
function downloadFile(){
    var filePath = $(this).closest('tr').attr('path');
    var url = "https://api-content.dropbox.com/1/files/auto"+filePath;
    var headers = {
        Authorization: 'Bearer ' + getAccessToken()
    };
    var args = {
        url: url,
        headers: headers,
        type: 'GET',
        success: function(data, textStatus, request)
        {
            var metadata = $.parseJSON(request.getResponseHeader('x-dropbox-metadata'));
            console.log(data);
        },
        error: function(jqXHR){
            console.log(jqXHR);
        }
    };
    $.ajax(args);
}

1 个答案:

答案 0 :(得分:0)

应该使用chrome.downloads API:

来实现
function downloadFile() {
    var filePath = $(this).closest('tr').attr('path');
    var url = "https://api-content.dropbox.com/1/files/auto"+filePath;
    var headers = {
        Authorization: 'Bearer ' + getAccessToken()
    };
    var args = {
        url: url,
        headers: headers,
        method: 'GET', // note the attribut name change
        filename: "FillThisOut" // may not be required
    };
    chrome.downloads.download(args, function(downloadId) {
        /* additional processing of the download, if needed */
    });
}
相关问题