element/packages/upload/src/index.vue

215 lines
4.4 KiB
Vue
Raw Normal View History

2016-08-20 10:27:37 +00:00
<script>
import UploadList from './upload-list';
import Upload from './upload';
2016-08-21 07:12:56 +00:00
import IframeUpload from './iframe-upload';
2016-10-25 13:35:41 +00:00
import ElProgress from 'element-ui/packages/progress';
2016-08-20 10:27:37 +00:00
function noop() {
}
export default {
name: 'el-upload',
components: {
ElProgress,
UploadList,
2016-08-21 07:12:56 +00:00
Upload,
IframeUpload
2016-08-20 10:27:37 +00:00
},
props: {
action: {
type: String,
required: true
},
headers: {
type: Object,
default() {
return {};
2016-08-20 10:27:37 +00:00
}
},
2016-10-12 02:31:50 +00:00
data: Object,
multiple: Boolean,
2016-08-20 10:27:37 +00:00
name: {
type: String,
default: 'file'
},
2016-10-12 02:31:50 +00:00
withCredentials: Boolean,
2016-08-20 10:27:37 +00:00
thumbnailMode: Boolean,
showUploadList: {
type: Boolean,
default: true
},
accept: String,
type: {
type: String,
default: 'select'
},
beforeUpload: Function,
onRemove: {
type: Function,
default: noop
},
onChange: {
type: Function,
default: noop
},
onPreview: {
type: Function,
default: noop
2016-09-01 05:49:09 +00:00
},
onSuccess: {
type: Function,
default: noop
},
onError: {
type: Function,
default: noop
2016-08-20 10:27:37 +00:00
}
},
data() {
return {
fileList: [],
2016-08-20 10:27:37 +00:00
dragOver: false,
draging: false,
tempIndex: 1
};
},
methods: {
2016-09-01 05:49:09 +00:00
handleStart(file) {
2016-08-20 10:27:37 +00:00
file.uid = Date.now() + this.tempIndex++;
let _file = {
status: 'uploading',
name: file.name,
size: file.size,
percentage: 0,
uid: file.uid,
showProgress: true
};
if (this.thumbnailMode) {
try {
_file.url = URL.createObjectURL(file);
} catch (err) {
console.log(err);
return;
}
}
this.fileList.push(_file);
2016-08-20 10:27:37 +00:00
},
2016-09-01 05:49:09 +00:00
handleProgress(ev, file) {
2016-08-20 10:27:37 +00:00
var _file = this.getFile(file);
_file.percentage = ev.percent || 0;
2016-08-20 10:27:37 +00:00
},
2016-09-01 05:49:09 +00:00
handleSuccess(res, file) {
2016-08-20 10:27:37 +00:00
var _file = this.getFile(file);
2016-08-21 07:12:56 +00:00
if (_file) {
_file.status = 'finished';
_file.response = res;
2016-08-20 10:27:37 +00:00
this.onSuccess(res, _file, this.fileList);
2016-09-01 05:49:09 +00:00
2016-08-21 07:12:56 +00:00
setTimeout(() => {
_file.showProgress = false;
}, 1000);
}
2016-08-20 10:27:37 +00:00
},
handleError(err, response, file) {
2016-08-20 10:27:37 +00:00
var _file = this.getFile(file);
var fileList = this.fileList;
2016-08-20 10:27:37 +00:00
_file.status = 'fail';
fileList.splice(fileList.indexOf(_file), 1);
2016-09-01 05:49:09 +00:00
this.onError(err, response, file);
2016-08-20 10:27:37 +00:00
},
handleRemove(file) {
var fileList = this.fileList;
2016-08-20 10:27:37 +00:00
fileList.splice(fileList.indexOf(file), 1);
this.onRemove(file, fileList);
},
getFile(file) {
var fileList = this.fileList;
2016-08-20 10:27:37 +00:00
var target;
fileList.every(item => {
target = file.uid === item.uid ? item : null;
return !target;
});
return target;
},
handlePreview(file) {
if (file.status === 'finished') {
this.onPreview(file);
}
},
clearFiles() {
this.fileList = [];
2016-08-20 10:27:37 +00:00
}
},
render(h) {
var uploadList;
2016-08-21 07:12:56 +00:00
if (this.showUploadList && !this.thumbnailMode && this.fileList.length) {
2016-08-20 10:27:37 +00:00
uploadList = (
<UploadList
files={this.fileList}
2016-08-20 10:27:37 +00:00
on-remove={this.handleRemove}
on-preview={this.handlePreview}>
</UploadList>
);
}
var props = {
props: {
2016-08-21 07:12:56 +00:00
type: this.type,
2016-08-20 10:27:37 +00:00
action: this.action,
multiple: this.multiple,
'before-upload': this.beforeUpload,
'with-credentials': this.withCredentials,
2016-09-09 03:38:54 +00:00
headers: this.headers,
2016-08-20 10:27:37 +00:00
name: this.name,
2016-10-12 02:31:50 +00:00
data: this.data,
2016-08-20 10:27:37 +00:00
accept: this.thumbnailMode ? 'image/*' : this.accept,
2016-09-01 05:49:09 +00:00
'on-start': this.handleStart,
'on-progress': this.handleProgress,
'on-success': this.handleSuccess,
'on-error': this.handleError,
2016-08-20 10:27:37 +00:00
'on-preview': this.handlePreview,
'on-remove': this.handleRemove
},
ref: 'upload-inner'
2016-08-20 10:27:37 +00:00
};
2016-08-31 03:25:02 +00:00
var uploadComponent = typeof FormData !== 'undefined'
2016-08-21 07:12:56 +00:00
? <upload {...props}>{this.$slots.default}</upload>
: <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
2016-08-20 10:27:37 +00:00
if (this.type === 'select') {
return (
<div class="el-upload">
{uploadList}
2016-08-21 07:12:56 +00:00
{uploadComponent}
2016-08-20 10:27:37 +00:00
{this.$slots.tip}
</div>
);
}
if (this.type === 'drag') {
return (
<div class="el-upload">
2016-08-21 07:12:56 +00:00
{uploadComponent}
2016-08-20 10:27:37 +00:00
{this.$slots.tip}
{uploadList}
</div>
);
}
}
};
</script>