Vue 3 File Upload Component

Build a file upload component in Vue 3 with drag-and-drop, progress bar, multiple file support, preview thumbnails for images, and automatic upload with.

JavascriptVueFile Upload

Code

snippet
import { ref } from 'vue';

export default {
  setup() {
    const files = ref([]);
    const progress = ref({});

    const handleDrop = (e) => {
      files.value = [...e.dataTransfer.files];
      files.value.forEach(upload);
    };

    const upload = async (file) => {
      const fd = new FormData();
      fd.append('file', file);
      const res = await fetch('/api/upload', { method: 'POST', body: fd });
      progress.value[file.name] = 100;
      return res.json();
    };

    return { files, progress, handleDrop };
  },
}

How the snippet works

Vue 3 File Upload Component keeps everything in the functions handleDrop and upload.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. The import line is the dependency contract: ref comes from vue and nothing else.

The result comes back as an object carrying files, progress.

Points of care

  • vue must be installed first — the snippet imports it directly.
  • fetch exist only in browsers — a Node runtime has no shims for them out of the box.

Applying it

Signature: const handleDrop = (e)

The call returns files, progress — destructure what you need at the call site.

Statements that matter

const res = await fetch('/api/upload', { method: 'POST', body: fd });
return { files, progress, handleDrop };
const handleDrop = (e) => {
  files.value = [...e.dataTransfer.files];
  files.value.forEach(upload);
};

FAQ

Does `handleDrop` modify its input?
No mutation happens — the function reads its input and returns a new value.
Are there third-party dependencies?
`vue` — install it with the package manager before running.