File Upload using Vue js Axios PHP

File Upload using Vue js Axios PHP

In this post we will give you information about File Upload using Vue js Axios PHP. Hear we will give you detail about File Upload using Vue js Axios PHPAnd how to use it also give you demo for it if it is necessary.

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

Also see:Vue JS Scroll to element in div using vue-scrollto

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 >

<div >

<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:

Also see:How to create component in vue js cli?

sudo npm run serve

I hope it can help you…

Hope this code and post will helped you for implement File Upload using Vue js Axios PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  78  =  81

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US