Next.js Discord

Discord Forum

Uploading image avatar as form data

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Hi Guys, this is the code i have:
export async function POST(request: Request) { try { const formData = await request.formData(); const file = formData.get("image");

When i try to upload image from React Native as blob, file gets logged as blank:
const formData = new FormData() formData.append('image', blob, filename) // Where Blob is taken from Expo library from taking photos from camera try { const response = await axios.post<Avatar>(${BASE_URL}/api/user/avatar, formData, config, ) return response.data } catch (error) { if (axios.isAxiosError(error)) { const axiosError = error as AxiosError if (axiosError.response?.status === 400) { // Handle the specific case where the status code is 400 throw new Error('Missing required data (userId or avatarData)') } else { // Handle other Axios errors or status codes as needed throw error } } else { // Handle other generic errors throw error } }

Any suggestions on what i could try?

0 Replies