123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <template>
- <div class="el-upload__inner"
- :class="{
- 'el-dragger': type === 'drag',
- 'is-dragOver': dragOver,
- 'is-showCover': showCover
- }"
- @click="handleClick"
- @drop.prevent="onDrop"
- @dragover.prevent="dragOver = true"
- @dragleave.prevent="dragOver = false">
- <slot v-if="!showCover"></slot>
- <cover :image="lastestFile" :on-preview="onPreview" :on-remove="onRemove" v-else></cover>
- <input class="el-upload__input" type="file" ref="input" @change="handleChange" :multiple="multiple" :accept="accept">
- </div>
- </template>
- <script>
- import ajax from './ajax';
- import Cover from './cover';
- export default {
- components: {
- Cover
- },
- props: {
- type: String,
- action: {
- type: String,
- required: true
- },
- name: {
- type: String,
- default: 'file'
- },
- data: Object,
- headers: Object,
- withCredentials: Boolean,
- multiple: Boolean,
- accept: String,
- onStart: Function,
- onProgress: Function,
- onSuccess: Function,
- onError: Function,
- beforeUpload: Function,
- onPreview: {
- type: Function,
- default: function() {}
- },
- onRemove: {
- type: Function,
- default: function() {}
- }
- },
- data() {
- return {
- dragOver: false,
- mouseover: false
- };
- },
- computed: {
- lastestFile() {
- var fileList = this.$parent.fileList;
- return fileList[fileList.length - 1];
- },
- showCover() {
- var file = this.lastestFile;
- return this.thumbnailMode && file && file.status !== 'fail';
- },
- thumbnailMode() {
- return this.$parent.thumbnailMode;
- }
- },
- methods: {
- isImage(str) {
- return str.indexOf('image') !== -1;
- },
- handleChange(ev) {
- const files = ev.target.files;
- if (!files) {
- return;
- }
- this.uploadFiles(files);
- this.$refs.input.value = null;
- },
- uploadFiles(files) {
- let postFiles = Array.prototype.slice.call(files);
- if (!this.multiple) { postFiles = postFiles.slice(0, 1); }
- if (postFiles.length === 0) { return; }
- postFiles.forEach(file => {
- let isImage = this.isImage(file.type);
- if (this.thumbnailMode && !isImage) {
- return;
- } else {
- this.upload(file);
- }
- });
- },
- upload(file) {
- if (!this.beforeUpload) {
- return this.post(file);
- }
- 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);
- }
- },
- 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,
- data: this.data,
- filename: this.name,
- onProgress: e => {
- this.onProgress(e, file);
- },
- onSuccess: res => {
- this.onSuccess(res, file);
- },
- onError: err => {
- this.onError(err, file);
- }
- });
- },
- onDrop(e) {
- this.dragOver = false;
- this.uploadFiles(e.dataTransfer.files);
- },
- handleClick() {
- this.$refs.input.click();
- }
- }
- };
- </script>
|