File Upload using Vue js Axios PHP

By Hardik Savani | December 18, 2018 | | 2678 Viewer | Category : PHP Vue.JS

In this tutorial, i will show you files uploading with vuejs and axios. Vue and Axios are working together awesome for making HTTP requests. you can simply call api or ajax for post, image upload, file upload with vue and axios. back end side you can use framework that you want like php, laravel, codeigniter etc.

this tutorial will help to quick start to make file upload using vue an axios. i will explain step by step to create image upload with vue js and axios, so you don't need to worries about if you don't know how to use axios.

basically axios provide way to make http requests like GET, POST, PUT, DELETE etc. you don't require to write code for ajax like you always write for jquery. but axios provide method for ajax fire.

You need to just follow few step to get image upload example using vue js and axios.

Step 1: Create Vue App

first we need to create vue cli app using bellow command:

vue create myApp

Step 2: Install Axios

Here we need to install axios npm package that will allow to make http request.

npm install --save axios vue-axios

Step 3: Use Axios

We need to use Axios package in main.js file of vue js app.


import Vue from 'vue'

import App from './App.vue'

import axios from 'axios'

import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

Vue.config.productionTip = false

new Vue({

render: h => h(App),


Step 4: Update App.vue File

In this step, we need to update app.vue file, because i updated component so.



<div id="app">





import Example from './components/Example.vue'

export default {

name: 'app',

components: {





Step 5: Create Example Component

Here, we will create Example.vue component with following code.



<div class="container">

<div class="large-12 medium-12 small-12 cell">

<h1>Vue JS Axios - Image Upload using PHP API -</h1>


<input type="file" id="file" ref="file" v-on:change="onChangeFileUpload()"/>


<button v-on:click="submitForm()">Upload</button>





export default {


return {

file: ''



methods: {


let formData = new FormData();

formData.append('file', this.file);'http://localhost:8000/api.php',



headers: {

'Content-Type': 'multipart/form-data'











this.file = this.$refs.file.files[0];





Step 6: Create PHP API

in this step, we will create simple php file with "upload" folder on root directory and then we will use as api this file. so, simply add following code and run with localhost:8000 local server.



header('Access-Control-Allow-Origin: *');

if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES['file']['name'])) {

echo "done";



echo "failed";


Now you can run vue app by using following command:

sudo npm run serve

I hope it can help you...

***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: