The Extra Horizon SDK can be used on different platforms, each handling binary data slightly different. So we've collected a few examples on how to upload/download binary data for each of the supported platforms.
Web
In browsers the File and Blob classes can be used for uploads.
// A file token pointing to a text file containing 'Hello, world!'constfileToken='66030067d7342660dbc63303-49e4fa23-2079-4b91-acb1-5221ecee8393';constarrayBuffer=awaitexh.files.retrieve(fileToken);constcontent=awaitnewResponse(arrayBuffer).text();console.log(content); // Shows 'Hello, world!'
React Native
Currently React Native provides limited support for binary data upload using FormData. As this is the basis for our file upload, for now only uploading from the file system is properly supported.
import*as ImagePicker from'expo-image-picker';constfileName='myImage.jpeg'; // File name is not returned by the picker on iOSconstimagePickerResult=awaitImagePicker.launchCameraAsync();constimage=imagePickerResult.assets[0];constuploadResult=awaitexh.files.create(fileName, { uri:image.uri, name: fileName, type:image.mimeType,// Must be a valid MIME type, so not just `image.type`});
Download example
// A file token pointing to a text file containing 'Hello, world!'constfileToken='66030067d7342660dbc63303-49e4fa23-2079-4b91-acb1-5221ecee8393';constarrayBuffer=awaitexh.files.retrieve(fileToken);constcontent=awaitnewResponse(arrayBuffer).text();console.log(content); // Shows 'Hello, world!'
Node.js
The form-datapackage is used to allow strings, buffers and streams to be uploaded.
// A file token pointing to a text file containing 'Hello, world!'constfileToken='66030067d7342660dbc63303-49e4fa23-2079-4b91-acb1-5221ecee8393';constbuffer=awaitexh.files.retrieve(fileToken);constcontent=buffer.toString();console.log(content); // Shows 'Hello, world!'
Stream download example
// A file token pointing to a text file containing 'Hello, world!'constfileToken='66030067d7342660dbc63303-49e4fa23-2079-4b91-acb1-5221ecee8393';conststreamResponse=awaitexh.files.retrieveStream(fileToken);conststream=streamResponse.data;constbuffer=awaitnewPromise((resolve, reject) => {constchunks= [];stream.on('data', chunk =>chunks.push(chunk));stream.on('error', reject);stream.on('end', () =>resolve(Buffer.concat(chunks)));});constcontent=buffer.toString();console.log(content); // Shows 'Hello, world!'