Reimplement drag-n-drop media upload

Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>
parent 230d6232
<template> <template>
<div> <el-button-group>
<el-button type="success" class="new-folder" @click="dialogFormVisible = true"> <el-button type="success" class="new-folder" @click="dialogFormVisible = true">
<i class="fa fa-plus"></i> {{ trans('folders.create resource') }} <i class="fa fa-plus"></i> {{ trans('folders.create resource') }}
</el-button> </el-button>
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
</span> </span>
</el-dialog> </el-dialog>
</div> </el-button-group>
</template> </template>
<script> <script>
......
<template> <template>
<div class="row"> <el-upload
<div class="col-xs-12"> class="media-upload"
<el-upload drag
class="media-upload" :action="uploadUrl"
drag list-type="picture"
:action="uploadUrl" :show-file-list="false"
:on-preview="handlePreview" :http-request="uploadFile"
:on-remove="handleRemove" multiple
:on-success="handleSuccess" >
:file-list="fileList" <i class="el-icon-upload"></i>
:headers="requestHeaders" <div class="el-upload__text">Drop file here or <em>click to upload</em></div>
multiple> </el-upload>
<i class="el-icon-upload"></i>
<div class="el-upload__text">Drop file here or <em>click to upload</em></div>
</el-upload>
</div>
</div>
</template> </template>
<script> <script>
import axios from 'axios';
export default { export default {
props: {
parentId: { type: Number },
},
data() { data() {
return { return {
fileList: [], fileIsUploading: false,
}; };
}, },
computed: { computed: {
uploadUrl() { uploadUrl() {
return route('api.media.store').domain + route('api.media.store').url; return route('api.media.store').domain + route('api.media.store').url;
}, },
requestHeaders() {
const userApiToken = document.head.querySelector('meta[name="user-api-token"]');
return {
Authorization: `Bearer ${userApiToken.content}`,
};
},
}, },
methods: { methods: {
handleSuccess(response) { uploadFile(event) {
this.$events.emit('fileWasUploaded', response); this.fileIsUploading = true;
this.fileList = []; const data = new FormData();
data.append('parent_id', this.parentId);
data.append('file', event.file);
axios.post(route('api.media.store'), data).then((response) => {
this.$events.emit('fileWasUploaded', response);
this.$message({
type: 'success',
message: this.trans('media.file uploaded'),
});
this.fileIsUploading = false;
}, (error) => {
console.log(error.response.data);
this.fileIsUploading = false;
this.$notify.error({
title: 'Error',
message: error.response.data.errors.file[0],
});
});
}, },
handlePreview() {},
handleRemove() {},
}, },
mounted() {},
}; };
</script> </script>
<style> <style>
.media-upload {
margin-bottom: 10px;
}
.el-upload__input { .el-upload__input {
display: none !important; display: none !important;
} }
.el-upload--text {
display: block; .el-upload--picture, .el-upload--picture-card {
width: 100%;
height: 175px;
line-height: 100px;
border: none;
} }
.el-upload-dragger { .el-upload-dragger {
width: 100%; width: 100%;
height: 100%;
} }
.media-upload {
margin-bottom: 10px; .el-upload-dragger .el-upload__text {
position: absolute;
bottom: 0;
width: 100%;
}
.el-upload--text {
display: block;
} }
</style> </style>
...@@ -38,6 +38,7 @@ return [ ...@@ -38,6 +38,7 @@ return [
'create resource' => 'Create media', 'create resource' => 'Create media',
'edit resource' => 'Edit media', 'edit resource' => 'Edit media',
'destroy resource' => 'Delete media', 'destroy resource' => 'Delete media',
'file uploaded' => 'File uploaded successfully',
'file too large' => 'File is too large. Must be below :size MB.', 'file too large' => 'File is too large. Must be below :size MB.',
'some files not moved' => 'Some files were not moved', 'some files not moved' => 'Some files were not moved',
'files moved successfully' => 'Files moved successfully', 'files moved successfully' => 'Files moved successfully',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment