Jecelyn Yeen
In this article, we will talk about how to handle file uploads with VueJs. We will create an images uploader that allow user to upload single or multiple images file by drag and drop or select file dialog.
We will then upload the selected images and display them accordingly. We will also learn to filter the upload file type, for example, we only allow images, do not allow file type like PDF.
File upload consists of two parts: the UI (front-end) and the API (back-end). We will be using VueJs to handle the UI part. We need a backend application to accept the uploaded files. You may follow the backend tutorials or download and run either one of these server side application to handle file upload for your backend:-
We will be using File upload with Hapi.js as our backend throughout this articles. We will also learn the tricks to enable fake upload on the front-end.
We will be using vue-cli to scaffold Vue.js projects. We will be using the webpack-simple
project template.
# install cli
npm install vue-cli -g
# then create project, with sass
# follow the instructions to install all necessary dependencies
vue init webpack-simple file-upload-vue
Alright, all set. Let’s proceed to create our component.
We will write our code in App.vue
. Remove all the auto-generated code in the file.
<!-- App.vue -->
<!-- HTML Template -->
<template>
<div id="app">
<div class="container">
<!--UPLOAD-->
<form enctype="multipart/form-data" novalidate v-if="isInitial || isSaving">
<h1>Upload images</h1>
<div class="dropbox">
<input type="file" multiple :name="uploadFieldName" :disabled="isSaving" @change="filesChange($event.target.name, $event.target.files); fileCount = $event.target.files.length"
accept="image/*" class="input-file">
<p v-if="isInitial">
Drag your file(s) here to begin<br> or click to browse
</p>
<p v-if="isSaving">
Uploading {{ fileCount }} files...
</p>
</div>
</form>
</div>
</template>
<!-- Javascript -->
<script>
</script>
<!-- SASS styling -->
<style lang="scss">
</style>
Notes:-
App.vue
component consists of 3 part: template (HTML), script (Javascript) and styles (SASS).enctype="multipart/form-data"
is important. To enable file upload, this attribute must be set. Learn more about enctype here.<input type="file" />
to accept file upload. The property multiple
indicate it’s allow multiple file upload. Remove it for single file upload.change
event. Whenever the file input change (someone drop or select files), we will trigger the filesChange
function and pass in the control name and selected files $event.target.files
, and then upload to server.accept="image/*"
.fileCount
of the when file changes. We use the fileCount
variable in displaying number of files uploading Uploading {{ fileCount }} files...
.Now, that’s the interesting part. Currently, our component look like this:
We need to transform it to look like this:
Let’s style it!
<!-- App.vue -->
...
<!-- SASS styling -->
<style lang="scss">
.dropbox {
outline: 2px dashed grey; /* the dash box */
outline-offset: -10px;
background: lightcyan;
color: dimgray;
padding: 10px 10px;
min-height: 200px; /* minimum height */
position: relative;
cursor: pointer;
}
.input-file {
opacity: 0; /* invisible but it's there! */
width: 100%;
height: 200px;
position: absolute;
cursor: pointer;
}
.dropbox:hover {
background: lightblue; /* when mouse over to the drop zone, change color */
}
.dropbox p {
font-size: 1.2em;
text-align: center;
padding: 50px 0;
}
</style>
With only few lines of scss, our component looks prettier now.
Notes:-
opacity: 0
style. This doesn’t hide the file input, it just make it invisible.dropbox
css class. We make it look like a drop file zone surround with dash.Let’s proceed to code our component.
<!-- App.vue -->
...
<!-- Javascript -->
<script>
import { upload } from './file-upload.service';
const STATUS_INITIAL = 0, STATUS_SAVING = 1, STATUS_SUCCESS = 2, STATUS_FAILED = 3;
export default {
name: 'app',
data() {
return {
uploadedFiles: [],
uploadError: null,
currentStatus: null,
uploadFieldName: 'photos'
}
},
computed: {
isInitial() {
return this.currentStatus === STATUS_INITIAL;
},
isSaving() {
return this.currentStatus === STATUS_SAVING;
},
isSuccess() {
return this.currentStatus === STATUS_SUCCESS;
},
isFailed() {
return this.currentStatus === STATUS_FAILED;
}
},
methods: {
reset() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
this.uploadedFiles = [];
this.uploadError = null;
},
save(formData) {
// upload data to the server
this.currentStatus = STATUS_SAVING;
upload(formData)
.then(x => {
this.uploadedFiles = [].concat(x);
this.currentStatus = STATUS_SUCCESS;
})
.catch(err => {
this.uploadError = err.response;
this.currentStatus = STATUS_FAILED;
});
},
filesChange(fieldName, fileList) {
// handle file changes
const formData = new FormData();
if (!fileList.length) return;
// append the files to FormData
Array
.from(Array(fileList.length).keys())
.map(x => {
formData.append(fieldName, fileList[x], fileList[x].name);
});
// save it
this.save(formData);
}
},
mounted() {
this.reset();
},
}
</script>
Notes:-
photos
. That’s our file input field name.filesChange
function. FileList
is an object returned by the files property of the HTML <input> element. It allow us to access the list of files selected with the <input type=“file”> element. Learn more [here]((https://developer.mozilla.org/en/docs/Web/API/FileList).FormData
, and append all our photos
files to it. FormData
interface provides a way to easily construct a set of key/value pairs representing form fields and their values. Learn more here.save
function will call our file upload service (hang on, we will create the service next!). We also set the status according to the result.mount()
is the vue component life cycle hook. During that point, we will set our component status to initial state.Let’s proceed to create our service. We will be using axios to make HTTP calls.
# install axios
npm install axios --save
// file-upload.service.js
import * as axios from 'axios';
const BASE_URL = 'http://localhost:3001';
function upload(formData) {
const url = `${BASE_URL}/photos/upload`;
return axios.post(url, formData)
// get data
.then(x => x.data)
// add url field
.then(x => x.map(img => Object.assign({},
img, { url: `${BASE_URL}/images/${img.id}` })));
}
export { upload }
Nothing much, the code is pretty expressive itself. We upload the files, wait for the result, map it accordingly.
You may run the application now with npm run dev
command. Try uploading a couple of images, and it’s working! (Remember to start your backend server)
We can upload the files successfully now. However, there’s no indication in UI. Let’s update our HTML template.
<!-- App.vue -->
<!-- HTML Template -->
<template>
<div id="app">
<div class="container">
...form...
<!--SUCCESS-->
<div v-if="isSuccess">
<h2>Uploaded {{ uploadedFiles.length }} file(s) successfully.</h2>
<p>
<a href="javascript:void(0)" @click="reset()">Upload again</a>
</p>
<ul class="list-unstyled">
<li v-for="item in uploadedFiles">
<img :src="item.url" class="img-responsive img-thumbnail" :alt="item.originalName">
</li>
</ul>
</div>
<!--FAILED-->
<div v-if="isFailed">
<h2>Uploaded failed.</h2>
<p>
<a href="javascript:void(0)" @click="reset()">Try again</a>
</p>
<pre>{{ uploadError }}</pre>
</div>
</div>
</div>
</template>
Notes:-
If you are lazy to start the back-end application (Hapi, Express, etc) to handle file upload. Here is a fake service to replace the file upload service.
// file-upload.fake.service.js
function upload(formData) {
const photos = formData.getAll('photos');
const promises = photos.map((x) => getImage(x)
.then(img => ({
id: img,
originalName: x.name,
fileName: x.name,
url: img
})));
return Promise.all(promises);
}
function getImage(file) {
return new Promise((resolve, reject) => {
const fReader = new FileReader();
const img = document.createElement('img');
fReader.onload = () => {
img.src = fReader.result;
resolve(getBase64Image(img));
}
fReader.readAsDataURL(file);
})
}
function getBase64Image(img) {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const dataURL = canvas.toDataURL('image/png');
return dataURL;
}
export { upload }
Came across this solution in this Stackoverflow post. Pretty useful. My online demo is using this service.
Basically, what the code do is read the source, draw it in canvas, and save it as data url with the canvas toDataURL
function. Learn more about canvas here.
Now you can swap the real service with the fake one.
<!-- App.vue -->
...
<!-- Javascript -->
<script>
// swap as you need
import { upload } from './file-upload.fake.service'; // fake service
// import { upload } from './file-upload.service'; // real service
</script>
...
Done! Stop your backend API, refresh your browser, you should see our app is still working, calling fake service instead.
Sometimes, you may want to delay the promises to see the state changes. In our case, the file upload may complete too fast. Let’s write a helper function for that.
// utils.js
// utils to delay promise
function wait(ms) {
return (x) => {
return new Promise(resolve => setTimeout(() => resolve(x), ms));
};
}
export { wait }
Then, you can use it in your component
<!-- App.vue -->
...
<!-- Javascript -->
<script>
import { wait } from './utils';
...
save(formData) {
....
upload(formData)
.then(wait(1500)) // DEV ONLY: wait for 1.5s
.then(x => {
this.uploadedFiles = [].concat(x);
this.currentStatus = STATUS_SUCCESS;
})
...
},
</script>
That’s it. This is how you can handle file upload without using any 3rd party libraries and plugins in Vue. It isn’t that hard right?
Happy coding!
The UI (Front-end)
The API (Back-end) Tutorials and Sourcode
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
All images are dead. The CDN blocks access to images
Nice article, there is however a bug in file-upload.fake.service.js.
Image loading is async so you need to handle the resolve in img.onload
FIX: … function getImage( …
fReader.onload = () => { img.onload = () => { resolve(getBase64Image(img)) } img.src = fReader.result; }