Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read base64 image from multipart #273

Closed
slabworwide opened this issue Jun 6, 2024 · 1 comment
Closed

How to read base64 image from multipart #273

slabworwide opened this issue Jun 6, 2024 · 1 comment

Comments

@slabworwide
Copy link

Hello, Could you please guide me how to extract base64 image from multipart.
In My code is below uploadToCloud function is responsible to upload files in cloud. It needs one variable mainly from multipart i.e base64 image. Now I am passing just passing key, imageType because I could not extract base64 image from field.file.

I am looking forward to get replies against my below queries:-
1.If I could read the buffer from field.file then I could encode it to base64. Could you tell me I could read the buffer.
2.How can I reproduce the error FILES_LIMIT_REACHED. Not mentioned in docs.
3. What is the advantage of multipart over normal method where hyper-express router receives a base64 image string as body from the client. On the other hand, in multipart I am sending the image from client as formdata.

 let image_file;
  let image_type;
  try {
    await request.multipart(async (field) => {
      if (field.file && field.mime_type.startsWith('image/')) {
        image_file = field.file.name
        image_type = field.mime_type

     
        //passing only jpeg, jpg & png type image            
        if (field.mime_type === 'image/jpeg' || field.mime_type === 'image/jpg' || field.mime_type === 'image/png') {
          await field.write(image_file)
        }
      }
    })

  } catch (error) {
    console.log(error);
    if (typeof error === 'FILES_LIMIT_REACHED') {
      return response.status(403).send('You sent too many files! Try again.')
    } else {
      return response.status(500).send('Oops! An uncaught error occured on our end.')
    }
  }

  if (image_file) {
    const { file } = await uploadToCloud({ key: image_file, imageType: image_type })
    console.log(file);
    return response.send("Successfully uploaded")
  } else {
    return response.status(400).send('No profile image was received. Please try again.');
  }
@slabworwide
Copy link
Author

slabworwide commented Jun 7, 2024

Hi, I found the base64 image from the stream. Now I can use it. Above issue is resolved

Thanks for creating the awesome framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant