Skywalker
Skywalker

Reputation: 5194

Dropbox API - Force Direct download

I using the dropbox API to upload files into dropbox and then generate a shareable links and sending those to the users.

But the issue is I want to force download the file instead of preview via dropbox share link.

I know I can force download setting ?dl=1 at the end of the link but the shareable link generated by dropbox comes back with ?dl=0

Code:

 request.put('https://api-content.dropbox.com/1/files_put/auto/reports/' + req.body.Name +'.pdf', {
                headers: {
                    Authorization: 'TOKEN',
                    'Content-Type': 'application/pdf'
                },
                body: content
            }, function optionalCallback(err, httpResponse, bodymsg) {
                if (err) {
                    console.log(err);
                }
                else {
                    console.log("File uploaded to dropbox successfully!");
                    fs.unlink(temp_dir + 'report.pdf', function(err) {
                        if (err)
                            throw err;
                        else {
                            console.log("file deleted from server!");
                        }
                    });
                    request.post('https://api.dropboxapi.com/1/shares/auto/reports/' + req.body.Name + '.pdf'+ '?short_url=false?dl=1', {
                        headers: {
                            Authorization: 'TOKEN'
                        }
                    }, function optionalCallback(err, httpResponse, bodymsg) {
                        if (err) {
                            console.log(err);
                        }
                        else {
                            console.log('Shared link 2 ' + JSON.parse(httpResponse.body).url);
                            res.json(JSON.parse(httpResponse.body).url);
                        }
                    });

                }
        });

I am using the V1 of dropbox API which will be deprecated soon but for now I need to use it.

Upvotes: 0

Views: 982

Answers (2)

adasq
adasq

Reputation: 106

You can take advantage of streams and just redirect download stream into client. Here is an example using my minimalistic dropbox-v2-api wrapper and hapijs route configuration:

{
    path: '/getFile',
    method: 'GET',
    handler: (request, response) => {
        dropbox({
            resource: 'files/download',
            parameters: {
                path: '/dropbox/image.jpg'
            }
        }, (err, result) => {
            //download completed
        }).pipe(response); //piping file stream
    }
}

Upvotes: 0

Greg
Greg

Reputation: 16930

Using the dl=1 URL parameter is the correct way to force downloads on these links. You can find more information on this here:

https://www.dropbox.com/help/desktop-web/force-download

To do this properly, you should use an actual URL parser to parse the URL. Then, if it already has a dl parameter, set it to 1. If it doesn't, then add dl parameter set to 1.

Upvotes: 1

Related Questions