2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2016-08-20 10:27:37 +00:00
|
|
|
<div class="el-upload__inner"
|
|
|
|
:class="{
|
|
|
|
'el-dragger': type === 'drag',
|
|
|
|
'is-dragOver': dragOver,
|
2016-08-21 07:12:56 +00:00
|
|
|
'is-showCover': showCover
|
2016-08-20 10:27:37 +00:00
|
|
|
}"
|
2016-09-06 17:15:39 +00:00
|
|
|
@click="handleClick"
|
2016-08-20 10:27:37 +00:00
|
|
|
@drop.prevent="onDrop"
|
|
|
|
@dragover.prevent="dragOver = true"
|
2016-10-27 09:31:22 +00:00
|
|
|
@dragleave.prevent="dragOver = false">
|
2016-08-21 07:12:56 +00:00
|
|
|
<slot v-if="!showCover"></slot>
|
|
|
|
<cover :image="lastestFile" :on-preview="onPreview" :on-remove="onRemove" v-else></cover>
|
2016-08-20 10:27:37 +00:00
|
|
|
<input class="el-upload__input" type="file" ref="input" @change="handleChange" :multiple="multiple" :accept="accept">
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2016-08-20 10:27:37 +00:00
|
|
|
import ajax from './ajax';
|
2016-08-21 07:12:56 +00:00
|
|
|
import Cover from './cover';
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
export default {
|
2016-08-21 07:12:56 +00:00
|
|
|
components: {
|
|
|
|
Cover
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
2016-08-20 10:27:37 +00:00
|
|
|
type: String,
|
2016-07-27 06:15:02 +00:00
|
|
|
action: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
default: 'file'
|
|
|
|
},
|
2016-10-12 02:31:50 +00:00
|
|
|
data: Object,
|
2016-09-02 10:11:45 +00:00
|
|
|
headers: Object,
|
2016-08-20 10:27:37 +00:00
|
|
|
withCredentials: Boolean,
|
|
|
|
multiple: Boolean,
|
2016-07-27 06:15:02 +00:00
|
|
|
accept: String,
|
2016-08-20 10:27:37 +00:00
|
|
|
onStart: Function,
|
|
|
|
onProgress: Function,
|
|
|
|
onSuccess: Function,
|
|
|
|
onError: Function,
|
|
|
|
beforeUpload: Function,
|
|
|
|
onPreview: {
|
|
|
|
type: Function,
|
|
|
|
default: function() {}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
onRemove: {
|
|
|
|
type: Function,
|
|
|
|
default: function() {}
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
dragOver: false,
|
2016-08-20 10:27:37 +00:00
|
|
|
mouseover: false
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
2016-08-20 10:27:37 +00:00
|
|
|
lastestFile() {
|
2016-10-27 02:58:32 +00:00
|
|
|
var fileList = this.$parent.fileList;
|
|
|
|
return fileList[fileList.length - 1];
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-21 07:12:56 +00:00
|
|
|
showCover() {
|
2016-08-20 10:27:37 +00:00
|
|
|
var file = this.lastestFile;
|
2016-08-21 07:12:56 +00:00
|
|
|
return this.thumbnailMode && file && file.status !== 'fail';
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
thumbnailMode() {
|
|
|
|
return this.$parent.thumbnailMode;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
isImage(str) {
|
|
|
|
return str.indexOf('image') !== -1;
|
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
handleChange(ev) {
|
|
|
|
const files = ev.target.files;
|
|
|
|
|
|
|
|
if (!files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.uploadFiles(files);
|
2016-10-27 02:58:32 +00:00
|
|
|
this.$refs.input.value = null;
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
uploadFiles(files) {
|
|
|
|
let postFiles = Array.prototype.slice.call(files);
|
|
|
|
if (!this.multiple) { postFiles = postFiles.slice(0, 1); }
|
|
|
|
|
|
|
|
if (postFiles.length === 0) { return; }
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-08-20 10:27:37 +00:00
|
|
|
postFiles.forEach(file => {
|
|
|
|
let isImage = this.isImage(file.type);
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-08-20 10:27:37 +00:00
|
|
|
if (this.thumbnailMode && !isImage) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this.upload(file);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
});
|
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
upload(file) {
|
|
|
|
if (!this.beforeUpload) {
|
|
|
|
return this.post(file);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-08-20 10:27:37 +00:00
|
|
|
const before = this.beforeUpload(file);
|
|
|
|
if (before && before.then) {
|
|
|
|
before.then(processedFile => {
|
|
|
|
if (Object.prototype.toString.call(processedFile) === '[object File]') {
|
|
|
|
this.post(processedFile);
|
|
|
|
} else {
|
|
|
|
this.post(file);
|
|
|
|
}
|
|
|
|
}, () => {
|
|
|
|
// this.$emit('cancel', file);
|
|
|
|
});
|
|
|
|
} else if (before !== false) {
|
|
|
|
this.post(file);
|
|
|
|
} else {
|
|
|
|
// this.$emit('cancel', file);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
post(file) {
|
|
|
|
this.onStart(file);
|
|
|
|
let formData = new FormData();
|
|
|
|
formData.append(this.name, file);
|
|
|
|
|
|
|
|
ajax(this.action, {
|
|
|
|
headers: this.headers,
|
|
|
|
withCredentials: this.withCredentials,
|
|
|
|
file: file,
|
2016-10-12 02:31:50 +00:00
|
|
|
data: this.data,
|
2016-08-20 10:27:37 +00:00
|
|
|
filename: this.name,
|
|
|
|
onProgress: e => {
|
|
|
|
this.onProgress(e, file);
|
|
|
|
},
|
|
|
|
onSuccess: res => {
|
|
|
|
this.onSuccess(res, file);
|
|
|
|
},
|
|
|
|
onError: err => {
|
|
|
|
this.onError(err, file);
|
|
|
|
}
|
|
|
|
});
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-08-20 10:27:37 +00:00
|
|
|
onDrop(e) {
|
|
|
|
this.dragOver = false;
|
|
|
|
this.uploadFiles(e.dataTransfer.files);
|
2016-09-06 17:15:39 +00:00
|
|
|
},
|
|
|
|
handleClick() {
|
|
|
|
this.$refs.input.click();
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|