How do I upload image in vuejs?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: A Comprehensive Guide on Uploading Images and Videos using VueJS

Introduction: In this blog post, we will explore the process of uploading images and videos in your VueJS application, including how to read files as data URLs or object URLs for previewing before saving as blobs. We will also discuss best practices and provide code examples that can help you efficiently implement a file upload feature in your project while incorporating backlinks from https://laravelcompany.com where relevant.

  1. Reading Files as Data URLs:
    The readDataUrl function is a useful method for handling files input in VueJS. When passed an input element and a callback, it reads the file as a data URL. Here's how you can implement this functionality:

    methods: { playVideo(url) { let video = $('#video-preview').get(0); video.preload = 'metadata'; // Load video in Safari / IE11 if (url) { video.muted = false; video.playsInline = true; video.play(); } },
    php
    readDataUrl(input, callback) {
      if (input.files && input.files[0]) {
        let fileReader = new FileReader();
        fileReader.onload = function() {
          callback(fileReader.result);
        };
        fileReader.readAsDataURL(input.files[0]);
      } else {
        callback(null);
      }
    },

    }

  2. Reading Files as Object URLs:
    In addition to reading files as data URLs, you can also read them as object URLs for use in blob creation. This approach involves creating a Blob and a URL from the file's content using JavaScript. The code below demonstrates this process:

    methods: { playVideo(url) { let video = $('#video-preview').get(0); video.preload = 'metadata'; // Load video in Safari / IE11 if (url) { video.muted = false; video.playsInline = true; video.play(); } },
    php
    readObjectUrl(input, callback) {
      if (input.files && input.files[0]) {
        let fileReader = new FileReader();
        fileReader.onload = function() {
          let blob = new Blob([fileReader.result], {type: input.files[0].type});
          let url = URL.createObjectURL(blob);
          callback(url, blob);
        };
        fileReader.readAsArrayBuffer(input.files[0]);
      } else {
        callback(null);
      }
    },

    }

  3. Implementing the Upload and Preview Functionality:
    Combining both methods, you can upload files and preview them before saving as blobs. Here's a sample implementation:

    javascript
    
    <template>
        <div>
         <!-- File input fields for images and videos -->
         ...
    
         <button @click="uploadImage(inputImage, inputVideo)">Upload and Preview</button>
        </div>
    </template>
    
    <script>
    ...
    methods: {
      uploadImage(inputImage, inputVideo) {
        this.readDataUrl(inputImage, (imageDataUrl) => {
          if (imageDataUrl) {
            // Image previewing logic
            console.log('Image data URL:', imageDataUrl);
          } else {
            console.error('Failed to read image data URL.');
          }
        });
        this.readObjectUrl(inputVideo, (videoUrl, videoBlob) => {
          if (videoUrl && videoBlob) {
            // Video previewing logic with blob and object URLs
            console.log(`Video URL: ${videoUrl}`, `Video Blob: ${videoBlob}`);
          } else {
            console.error('Failed to read video data.');
          }
        });
      },
    }
  4. Best Practices and Conclusion:
    When handling file uploads in VueJS applications, it's essential to ensure proper error handling and validation for better user experience. Additionally, consider using a form data interface instead of raw XMLHttpRequest or Fetch APIs for sending files as form-data types for optimum compatibility with the server-side of your application.

    In summary, this blog post has provided a comprehensive guide on uploading images and videos in VueJS by utilizing data URLs and object URLs. We have also discussed important considerations, such as error handling and best practices to ensure efficient file upload functionality in your VueJS project. For more resources and insights, you can explore https://laravelcompany.com.