Skip to content

Instantly share code, notes, and snippets.

@biancadragomir
Created March 24, 2021 14:24
Show Gist options
  • Save biancadragomir/d854223a0cdc5c76869d1083250b655a to your computer and use it in GitHub Desktop.
Save biancadragomir/d854223a0cdc5c76869d1083250b655a to your computer and use it in GitHub Desktop.
Using RNFetchBlob for a put/post multipart request
export const uploadFile = async (
record: Record,
date: string,
description: string,
recordId?: string
) => {
var headers = await defaultHeaders();
headers["Content-Type"] = "multipart/form-data";
const realPath =
Platform.OS === "ios"
? decodeURIComponent(record.file.url.replace("file://", ""))
: record.file.url;
const baseRecordUploadUrl = `${Config.API_URL}/upload-file`;
const multipartParams = [
{
name: "files.file",
filename: record.file.name,
data: RNFetchBlob.wrap(realPath),
},
{
name: "data",
data: JSON.stringify({
date: date,
description: description,
}),
},
];
await RNFetchBlob.fetch(
!recordId ? "post" : "put",
!recordId ? baseRecordUploadUrl : `${baseRecordUploadUrl}/${recordId}`,
headers as { [key: string]: string },
multipartParams
)
.then((resp) => {
console.log(resp);
})
.catch((err) => {
console.log(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment