React Native
React Native
Download File from Dropbox into a String Variable
See more Dropbox Examples
Demonstrates how to download a file from Dropbox directly into a string variable.Chilkat React Native Downloads
import { CkDateTime, DtObj, JsonObject, Rest } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.
const rest = new Rest();
// Connect to Dropbox
try {
await rest.connectAsync('content.dropboxapi.com', 443, true, true);
} catch {
console.log(rest.lastErrorText);
return;
}
// Add request headers.
rest.addHeader('Authorization', 'Bearer DROPBOX_ACCESS_TOKEN');
// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// {
// "path": "/jack.txt",
// }
const json = new JsonObject();
json.appendString('path', '/jack.txt');
rest.addHeader('Dropbox-API-Arg', json.emit());
// The content of the file on Dropbox is returned.
let fileContent: string;
try {
fileContent = await rest.fullRequestNoBodyAsync('POST', '/2/files/download');
} catch {
console.log(rest.lastErrorText);
return;
}
// When successful, Dropbox responds with a 200 response code.
if (rest.responseStatusCode !== 200) {
// Examine the request/response to see what happened.
console.log(`response status code = ${rest.responseStatusCode}`);
console.log(`response status text = ${rest.responseStatusText}`);
console.log(`response header: ${rest.responseHeader}`);
console.log(`response body (if any): ${fileContent}`);
console.log('---');
console.log(`LastRequestStartLine: ${rest.lastRequestStartLine}`);
console.log(`LastRequestHeader: ${rest.lastRequestHeader}`);
return;
}
// Show the file content that was downloaded:
console.log(fileContent);
console.log('----');
// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information. For example:
const apiResult = rest.responseHdrByName('dropbox-api-result');
console.log(apiResult);
// In this case, the pretty-formatted dropbox-api-result JSON looks like this:
// {
// "name": "jack.txt",
// "path_lower": "/jack.txt",
// "path_display": "/jack.txt",
// "id": "id:yqx4-tE_NKAAAAAAAAAAAQ",
// "client_modified": "2016-06-02T20:42:11Z",
// "server_modified": "2016-06-02T20:42:11Z",
// "rev": "8482db15f",
// "size": 42
// }
// Load the JSON, pretty-print it, and demonstrate how to get some values...
const jsonResult = new JsonObject();
jsonResult.emitCompact = false;
jsonResult.load(apiResult);
// Show the JSON pretty-printed...
console.log(jsonResult.emit());
// Sample code to get data from the JSON response:
const size = jsonResult.intOf('size');
console.log(`size = ${size}`);
const rev = jsonResult.stringOf('rev');
console.log(`rev = ${rev}`);
const clientModified = jsonResult.stringOf('client_modified');
const ckdt = new CkDateTime();
ckdt.setFromTimestamp(clientModified);
const bLocalTime = true;
const dt = new DtObj();
ckdt.toDtObj(bLocalTime, dt);
console.log(`${dt.day}/${dt.month}/${dt.year} ${dt.hour}:${dt.minute}`);
}