【问题标题】:Authenticating with dropbox api oauth 2.0 with nodejs and jquery使用 nodejs 和 jquery 使用 dropbox api oauth 2.0 进行身份验证
【发布时间】:2018-10-03 09:39:27
【问题描述】:

控制器中的 Nodejs:

我使用了一个函数file_upload,它使用了一个参数content,文件将在其中传递。在这段代码中,当我尝试控制台记录 req.files 时,它显示了 null。所以我的问题是,我的文件是否通过内容参数?如果不是,那么传递文件的方式是什么?

module.exports.fileUpload = function(req, res){
    if(!req.files){
        console.log('File not uploaded!');
    } else {
        upload_file(req.file);
    }
};

var token = << token here >>;

function upload_file(content){
    console.log(content);
    var options = {
        url: 'https://content.dropboxapi.com/2/files/upload',
        type: 'POST',
        headers: {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/octet-stream',
            'Dropbox-API-Arg': {'path': '/' + content, 'mute': false, 'mode': 'add', 'autorename': true}
        },
        body: content
    }
    return options;
};

我正在使用 jquery post 方法上传文件并将请求发送到控制器。

HTML

    <form class="form-control" enctype="multipart/form-data">
        <input type="file" class="input-form" name="file" id="file">
        <button type="submit" class="upload-btn" id="upload-btn">Upload</button>
</form>

JQuery:

$('#upload-btn').click(function(){
        var inputFile = $('#file')[0].files[0];
        var formData = new FormData();
        formData.append('file', inputFile, inputFile.name);

        $.ajax({
            type: 'POST',
            url: '/dropbox',
            data: formData,
            processData: false,
            contentType: false
        }).done(function(response){
            alert(response);
        }).fail(function(response){
            alert(response.responseText);
        });
        return false;
    });

我使用 formData 来获取文件,但它仍然无法正常工作。

这就是我到目前为止所做的。你们能告诉我我的问题以及为什么它不起作用吗?

【问题讨论】:

  • 我认为您应该将问题标题编辑为更清晰的内容,例如 "Problem authentication with dropbox api" 例如

标签: jquery html node.js ejs dropbox-api


【解决方案1】:

我做了 Dropbox 文件上传,但我使用的是文件路径,也许你会得到一些东西..

  var _uploadFile = function (req,res) {

        __fileData = fs.createReadStream(Your file path);
        var url = 'https://content.dropboxapi.com/2/files/upload';
        var path = '{\"path\": \"' + folder + '/' + filename + '\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}';
        var options = {
            method: 'POST',
            url: url,
            headers: {
                'Authorization': 'Bearer ' + req.body.auth.access_token,
                'Content-Type': 'application/octet-stream',
                'Dropbox-API-Arg': path
            },
            body: __fileData
        };
        request(options, function (err, response) {
            if (err) {
                res.send({data: 'error'});
            } else if (JSON.parse(response.body).error_summary) {
                res.send({data: 'Please provide valid file path'});
            } else {
                res.send('success');
            }
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2021-01-16
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2014-07-28
    相关资源
    最近更新 更多