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

[Feature request] Uploads support #848

Open
silvioprog opened this issue Jan 28, 2023 · 8 comments
Open

[Feature request] Uploads support #848

silvioprog opened this issue Jan 28, 2023 · 8 comments
Labels
enhancement New feature or request.

Comments

@silvioprog
Copy link

silvioprog commented Jan 28, 2023

Hi.

It would be nice to make it easy to handle uploads.

References:

@silvioprog silvioprog changed the title [Feature request] Upload support [Feature request] Uploads support Jan 28, 2023
@yusukebe yusukebe reopened this Jan 31, 2023
@yusukebe
Copy link
Member

Sorry! Discuss it here.

@charnould
Copy link

Hello,
Considering using Hono for a project, I didn't find in documentation how to handle file upload and have found this GitHub issue. Is file upload available in Hono?
Thanks for the project.

@yusukebe
Copy link
Member

@charnould

With the current version, you can write it as follows:

app.post('/upload', async (c) => {
  const { image } = await c.req.parseBody()

  if (image instanceof File) {
    console.log(image.name) // show the file name
    const buffer = await image.arrayBuffer()
    // do something with the buffer
  }

  return c.text('Uploaded!')
})

Written in #664 (comment)

@varHarrie
Copy link

@yusukebe Is there an example for hono client?

@yusukebe
Copy link
Member

@varHarrie

You can upload files using the form parameter, though type support is not fully available.

client.upload.$put({
  form: {
    image: field.files[0]
  }
})

@charnould
Copy link

@charnould

With the current version, you can write it as follows:

app.post('/upload', async (c) => {
  const { image } = await c.req.parseBody()

  if (image instanceof File) {
    console.log(image.name) // show the file name
    const buffer = await image.arrayBuffer()
    // do something with the buffer
  }

  return c.text('Uploaded!')
})

Written in #664 (comment)

It works like a charm! Thanks.

@yusukebe yusukebe mentioned this issue Aug 8, 2023
@joshamaju
Copy link

Shouldn't this be included in the documentation?

@krsbx
Copy link

krsbx commented Aug 31, 2023

@yusukebe do we need to create some sort of middleware like multer? Where it will automatically save the file to disk or to other storage like S3? If yes, then probably I'll make the PR on the middleware repo

@yusukebe yusukebe added the enhancement New feature or request. label Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

6 participants