How to upload a file in Vue.js

in Code Write a comment

In this tutorial, you’ll learn how to process and upload a file in Vue.js using form input field.

Let’s create a form field that will accept image files and make a POST request to the backend server.


<button class="btn btn-info" @click="onPickFile">Upload profile picture</button>
  style="display: none"

We’ve masked our input field to make upload button customisable. File input fields usually use default browser styling and not easily customisable.

Hi, I'm Renat 👋


As you can see there should be two methods called onPickFile() and onFilePicked(), let’s build them.


onPickFile () {
onFilePicked (event) {
  const files =
  let filename = files[0].name
  const fileReader = new FileReader()
  fileReader.addEventListener('load', () => {
    this.imageUrl = fileReader.result
  this.image = files[0]

Now when you click on the “Upload profile picture” button it will call the onFilePicked() method, which will programmatically trigger the file input field.

When you select a file to upload, onFilePicked() method will be called. This is where you can validate whether maximum file size was exceeded, file format etc.

If it passes validation the file will be assigned to the image filed in the data() object.

export default {
  data () {
    return {
      image: null

You can then write a method that will submit your file to the back-end server, e.g. firebase.

If you find this post useful, please let me know in the comments below. 
Renat Galyamov

Want to share this with your friends?

PS: Make sure you check other Vue.js tutorials, e.g. useful Vue.js filtershow to stop mouse event propagation in Vue.js and load external CSS file into Vue.js component.

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment



  1. How do you get the path of the uploaded file?

    Btw @Ross that’s a linting error, you can either cancel the `let filename = files[0].name` or disable no-unused-variables rule

    • Hi Fabio. The file is not uploaded to the backend yet, hence there’s no path. You can send it to the backend e.g. Firebase and get a path as a response.