Created
September 6, 2024 12:08
-
-
Save ricbermo/e0ecda7f4a6a6fb4877bf0a2bbfa7757 to your computer and use it in GitHub Desktop.
file upload
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
// 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