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

[Blob] handleUpload(event) and useUpload(path) #87

Closed
Atinux opened this issue Apr 25, 2024 — with Volta.net · 2 comments · Fixed by #99
Closed

[Blob] handleUpload(event) and useUpload(path) #87

Atinux opened this issue Apr 25, 2024 — with Volta.net · 2 comments · Fixed by #99
Assignees
Labels
enhancement New feature or request

Comments

Copy link
Contributor

Atinux commented Apr 25, 2024

On the servers-side:

// api/upload.post.ts
export default eventHandler(event => {
  // Do auth checks...
  return hubBlob().handleUpload(event, {
    formKey: 'file', // default
    multiple: true | false, // throw error if multiple files & false, default true
    // ... put() options
    // ensureBlob() options
  })
})

We should add a prefix option to put() to we can also have it in handleUpload(event)

On the app side:

<script setup>
async function upload(e: Event) {
  const { pathname } = await useUpload('/api/upload')(e.target.image)
}
</script>

<template>
  <div>
    <h3>Images</h3>
    <form @submit.prevent="upload">
      <label>Upload an image: <input type="file" name="image"></label>
      <button type="submit">
        Upload
      </button>
    </form>
</template>
@Atinux Atinux added the enhancement New feature or request label Apr 25, 2024 — with Volta.net
@Gerbuuun
Copy link
Contributor

I was just working on a way to preview the image before you upload it. Maybe you can add this to the useUpload composable?
Or is the pathname the location of the local image which can then be used in a <img> element?

Copy link
Contributor Author

Atinux commented Apr 26, 2024

You can use URL.createObjectURL(file) in the Vue part to display the blob:

<script setup lang="ts">
const avatar = ref()

function onFileChange (files: FileList | null) {
  if (!files?.length) return
  avatar.value = URL.createObjectURL(files[0])
}
</script>

<template>
  <form class="p-2">
    <UFormGroup name="avatar" label="Avatar" help="JPG or PNG, 1MB max.">
      <UAvatar :src="avatar" alt="avatar" size="lg" />
      <UInput ref="fileRef" type="file" accept=".jpg, .jpeg, .png" @change="onFileChange" />
    </UFormGroup>
  </form>
</template>

See a demo on https://stackblitz.com/~/edit/github-x6x77g

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

Successfully merging a pull request may close this issue.

3 participants