React Native
React Native
REST File Upload (multipart/form-data)
See more REST Examples
Demonstrates how to upload a file using multipart/form-data.Chilkat React Native Downloads
import { Rest, Stream } 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.
const rest = new Rest();
// Connect to the HTTP server using TLS.
const bTls = true;
const port = 443;
const bAutoReconnect = true;
// Make sure to replace "www.chilkatsoft.com" with your domain..
try {
await rest.connectAsync('www.chilkatsoft.com', port, bTls, bAutoReconnect);
} catch {
console.log(rest.lastErrorText);
return;
}
const fileStream = new Stream();
fileStream.sourceFile = 'qa_data/jpg/starfish.jpg';
rest.addHeader('Content-Type', 'multipart/form-data');
rest.partSelector = '1';
rest.addHeader('Content-Type', 'image/jpg');
rest.addHeader('Content-Disposition', 'form-data; name="filename"; filename="starfish.jpg"');
rest.setMultipartBodyStream(fileStream);
rest.partSelector = '0';
let responseBody: string;
try {
responseBody = await rest.fullRequestMultipartAsync('POST', '/the_uri_path_to_receive_the_upload');
} catch {
console.log(rest.lastErrorText);
return;
}
if (rest.responseStatusCode !== 200) {
console.log(`Received error response code: ${rest.responseStatusCode}`);
return;
}
console.log('File uploaded');
}