Created
March 24, 2021 14:24
-
-
Save biancadragomir/d854223a0cdc5c76869d1083250b655a to your computer and use it in GitHub Desktop.
Using RNFetchBlob for a put/post multipart request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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