Anis Sarker
Anis Sarker

Reputation: 1

Authenticating with dropbox api oauth 2.0 with nodejs and jquery

Nodejs in controller:

I have used a function file_upload using a parameter content in which the file will be pass. In this code when I tried to console.log the req.files it shows me null. So my problem, is my file passing through the content parameter or not? If not then what is the way to pass a file?

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

I'm using jquery post method to upload the file and to send the request to the controller.

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

I used formData to get the file but it still not working.

That's what I have done so far. Can you guys please tell me my problem and why it's not working?

Upvotes: 0

Views: 69

Answers (1)

Mohammad Raheem
Mohammad Raheem

Reputation: 1157

I did dropbox file upload but i am using file path, may be you will get something..

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

Upvotes: 0

Related Questions