The set of core methods.
@usage on()
@return Void
Used to create a new upload instance which is tracked in a global state.
Existing instances will not be overwritten so calling the on
method multiple times on the same instance will have no effect.
The method requires a name to be set for the instance and includes a series of callbacks that will trigger at each stage of a files upload.
Method options will always contain the current components context when using
this
. The set of full options for a new instance usingon
can be found on the Core Options page.
created() {
this.$upload.on('avatar', {
// All files
onSelect(files) {},
onStart() {},
onEnd() {},
// Each file
onQueue(file) {},
onProgress(file) {},
onUpload(file) {},
onError(file) {},
onSuccess(file) {},
onComplete(file) {},
});
},
methods: {
upload() {
this.$upload.option('avatar', 'url', 'users/1/avatar');
this.$upload.select();
}
},
@usage off()
@return Void
Destroy the upload instance. It's good to not leave these lingering around.
It should be fine to just use
on
without problems, but this allows to clean things up and remove performance issues.
beforeDestroy() {
this.$upload.off('avatar');
}
@usage reset()
@return Void
This will completely reset the upload instance.
An instance using
on
must be created first.
mounted() {
this.$upload.reset('avatar');
},
It's also useful to call reset()
for clearing an upload when using multiple
.
For instance a "clear" button.
<button
@click="$upload.reset('gallery')"
>
Clear
</button>
@usage select()
@return Void
This is used to manually trigger the browsers file selection popup/dialog.
By default the uploads will start once files are selected. Set the
startOnSelect
option tofalse
to prevent uploads from beginning automatically.
<button
@click="$upload.select('brand-logo')"
>
Update Photo
</button>
@usage start()
@return Void
This option is to be used in conjunction with the startOnSelect
option when it's set to false
to allow for a manual start of the uploads.
<img :src="brandLogo" />
<button
click="$upload.select('brand-logo')"
>
Select Photo
</button>
<button
click="$upload.start('brand-logo')"
>
Upload Photo
</button>
@usage files()
@return Array
Contains a set of "queue" arrays with the upload files at each different state.
The queues are all
, queue
, progress
, upload
, success
, error
.
The array will not reset after completing (use
reset
andonEnd
for that).
<div
v-for="file in $upload.files('gallery').progress"
>
{{ file.name }}: {{ file.percentComplete }}%
</div>
<div
v-for="file in $upload.files('gallery').queue"
>
{{ file.name }}: Queued for upload
</div>
@usage meta()
@return Object
Fetches some meta info about the current uploads.
The meta info is fully reactive an can be used directly in the templates.
The percentComplete
attribute keeps track of progress for the currently uploading files.
{{ $upload.meta('gallery').percentComplete }}%
The state
attribute will be either ready
, uploading
or complete
.
{{ $upload.meta('gallery').state }}
@usage percent()
@return Int
Shortcut for meta.percentComplete.
{{ $upload.percent('gallery') }}
@usage state()
@return String
Shortcut for meta.state.
{{ $upload.state('gallery') }}
@usage dropzone()
@return Object
For use with drag/drop files to upload into a "drop zone".
The primary use to be able to detect when the dropzone is activated by a file being dragged over the specified dropzone element.
A dropzone element id must be specified in the instance options for this to work.
<div
v-show="$upload.dropzone('gallery').active"
>
Drop files anywhere here to begin upload.
</div>
@usage errors()
@return Array
This returns the global error state for the upload instance.
This will not return the individual file errors.
<div
v-if="$upload.errors('gallery').length"
>
<div
v-for="error in $upload.errors('gallery')"
>
{{ error.code }}: {{ error.msg }}
</div>
</div>
@usage option()
@return Void
Update an option without resetting the upload instance.
This is useful to update a url on the fly or to add some additional body parameters to the post request.
methods: {
upload() {
this.$upload
.option('gallery', 'url', 'users/1/gallery');
this.$upload
.option('gallery', 'body', {
folder_id: 2
});
}
}