element/packages/upload/src/upload.vue

173 lines
3.8 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<script>
2017-02-08 11:05:34 +00:00
import merge from 'element-ui/src/utils/merge';
2016-08-20 10:27:37 +00:00
import ajax from './ajax';
2017-02-08 11:05:34 +00:00
import dragger from './dragger';
2016-07-27 06:15:02 +00:00
export default {
2017-02-08 11:05:34 +00:00
mixins: [dragger],
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,
2017-02-08 11:05:34 +00:00
draggable: Boolean,
2016-08-20 10:27:37 +00:00
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() {}
2017-02-08 11:05:34 +00:00
},
fileList: Array,
autoUpload: Boolean
2016-07-27 06:15:02 +00:00
},
data() {
return {
2016-08-20 10:27:37 +00:00
mouseover: false
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;
2017-02-08 11:05:34 +00:00
if (!files) return;
2016-08-20 10:27:37 +00:00
this.uploadFiles(files);
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
2017-02-08 11:05:34 +00:00
postFiles.forEach(rawFile => {
if (!this.thumbnailMode || this.isImage(rawFile.type)) {
this.onStart(rawFile);
if (this.autoUpload) this.upload(rawFile);
2016-08-20 10:27:37 +00:00
}
2016-07-27 06:15:02 +00:00
});
},
2017-02-08 11:05:34 +00:00
upload(rawFile, file) {
2016-08-20 10:27:37 +00:00
if (!this.beforeUpload) {
2017-02-08 11:05:34 +00:00
return this.post(rawFile);
2016-08-20 10:27:37 +00:00
}
2016-07-27 06:15:02 +00:00
2017-02-08 11:05:34 +00:00
const before = this.beforeUpload(rawFile);
2016-08-20 10:27:37 +00:00
if (before && before.then) {
before.then(processedFile => {
if (Object.prototype.toString.call(processedFile) === '[object File]') {
this.post(processedFile);
} else {
2017-02-08 11:05:34 +00:00
this.post(rawFile);
2016-08-20 10:27:37 +00:00
}
}, () => {
2017-02-08 11:05:34 +00:00
if (file) this.onRemove(file);
2016-08-20 10:27:37 +00:00
});
} else if (before !== false) {
2017-02-08 11:05:34 +00:00
this.post(rawFile);
2016-08-20 10:27:37 +00:00
} else {
2017-02-08 11:05:34 +00:00
if (file) this.onRemove(file);
2016-08-20 10:27:37 +00:00
}
2016-07-27 06:15:02 +00:00
},
2017-02-08 11:05:34 +00:00
post(rawFile) {
ajax({
2016-08-20 10:27:37 +00:00
headers: this.headers,
withCredentials: this.withCredentials,
2017-02-08 11:05:34 +00:00
file: rawFile,
2016-10-12 02:31:50 +00:00
data: this.data,
2016-08-20 10:27:37 +00:00
filename: this.name,
action: this.action,
2016-08-20 10:27:37 +00:00
onProgress: e => {
2017-02-08 11:05:34 +00:00
this.onProgress(e, rawFile);
2016-08-20 10:27:37 +00:00
},
onSuccess: res => {
2017-02-08 11:05:34 +00:00
this.onSuccess(res, rawFile);
2016-08-20 10:27:37 +00:00
},
onError: (err, response) => {
2017-02-08 11:05:34 +00:00
this.onError(err, response, rawFile);
2016-08-20 10:27:37 +00:00
}
});
2016-07-27 06:15:02 +00:00
},
2016-09-06 17:15:39 +00:00
handleClick() {
this.$refs.input.click();
2016-07-27 06:15:02 +00:00
}
2017-02-08 11:05:34 +00:00
},
render(h) {
let {
handleClick,
draggable,
onDrop,
showCover,
onPreview,
onRemove,
handleChange,
multiple,
accept,
lastestFile
} = this;
const data = {
class: {
'el-upload': true
},
on: {
click: handleClick
}
};
if (draggable) {
merge(data.on, {
dragover: (ev) => {
ev.preventDefault();
this.dragOver = true;
},
dragleave: (ev) => {
ev.preventDefault();
this.dragOver = false;
},
drop: (ev) => {
ev.preventDefault();
onDrop(ev);
}
});
merge(data.class, {
'el-upload--draggable': true,
'is-dragOver': this.dragOver,
'is-showCover': this.showCover
});
}
return (
<div {...data}>
{
showCover
? <cover image={lastestFile} on-preview={onPreview} on-remove={onRemove}></cover>
: this.$slots.default
}
<input class="el-upload__input" type="file" ref="input" on-change={handleChange} multiple={multiple} accept={accept}></input>
</div>
);
2016-07-27 06:15:02 +00:00
}
};
</script>