Skip to content

Instantly share code, notes, and snippets.

@ricbermo
Created September 6, 2024 12:08
Show Gist options
  • Save ricbermo/e0ecda7f4a6a6fb4877bf0a2bbfa7757 to your computer and use it in GitHub Desktop.
Save ricbermo/e0ecda7f4a6a6fb4877bf0a2bbfa7757 to your computer and use it in GitHub Desktop.
file upload
// algunos imports que te pueden servir
// import base64 from 'react-native-base64';
// import Blob from 'react-native-blob-util';
export async function uploadAttachments({
reportId,
appearanceId,
attachments = [],
setProgress,
}) {
try {
const authHeader = await getAuthToken();
const headers = {
Authorization: authHeader,
'Content-Type': 'multipart/form-data',
'User-Agent': USER_AGENT,
};
await Promise.each(attachments, function process({file, category}) {
const attachment = {
name: 'uploadedFile',
filename: file?.fileName,
type: file?.type,
data: Blob.wrap(file?.uri?.replace('file://', '')),
};
const url = getAttachmentURL({category, reportId, appearanceId});
return Blob.fetch('POST', `${url}?category=${category?.value}`, headers, [
attachment,
])?.uploadProgress((written = 1, total = 1) => {
if (setProgress) {
setProgress(written / total);
}
});
});
} catch (e) {
return Promise.reject('upload_error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment