Vue js File Upload Axios Php Example

PHP , Vue , Vue.Js

Nicesnippets

908

22-08-2020


Hi Guys,

In this tutorial,I will learn you how to use upload file with vue.js and php.you can easy ans simply use upload file with vue.js and php.

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 php.

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.

src/main.js

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),

}).$mount('#app')

Step 4: Update App.vue File

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

src/App.vue

<template>

<div id="app">

<Example></Example>

</div>

</template>

<script>

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

export default {

name: 'app',

components: {

Example

}

}

</script>

Step 5: Create Example Component

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

src/components/Example.vue

<template>

<div class="container">

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

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

<label>File

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

</label>

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

</div>

</div>

</template>

<script>

export default {

data(){

return {

file: ''

}

},

methods: {

submitForm(){

let formData = new FormData();

formData.append('file', this.file);

this.axios.post('http://localhost:8000/api.php',

formData,

{

headers: {

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

}

}

).then(function(data){

console.log(data.data);

})

.catch(function(){

console.log('FAILURE!!');

});

},

onChangeFileUpload(){

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

}

}

}

</script>

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.

api.php

<?php

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

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

echo "done";

exit;

}

echo "failed";

?>

Now you can run vue app by using following command:

sudo npm run serve

It Will help you...


Recommended Posts