123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <script>
- import UploadList from './upload-list';
- import Upload from './upload';
- import IframeUpload from './iframe-upload';
- import ElProgress from 'element-ui/packages/progress';
- function noop() {
- }
- export default {
- name: 'el-upload',
- components: {
- ElProgress,
- UploadList,
- Upload,
- IframeUpload
- },
- props: {
- action: {
- type: String,
- required: true
- },
- headers: {
- type: Object,
- default() {
- return {};
- }
- },
- data: Object,
- multiple: Boolean,
- name: {
- type: String,
- default: 'file'
- },
- withCredentials: Boolean,
- 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
- },
- onSuccess: {
- type: Function,
- default: noop
- },
- onError: {
- type: Function,
- default: noop
- },
- defaultFileList: {
- type: Array,
- default() {
- return [];
- }
- }
- },
- data() {
- return {
- fileList: [],
- dragOver: false,
- draging: false,
- tempIndex: 1
- };
- },
- methods: {
- handleStart(file) {
- 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);
- },
- handleProgress(ev, file) {
- var _file = this.getFile(file);
- _file.percentage = ev.percent || 0;
- },
- handleSuccess(res, file) {
- var _file = this.getFile(file);
- if (_file) {
- _file.status = 'finished';
- _file.response = res;
- this.onSuccess(res, _file, this.fileList);
- setTimeout(() => {
- _file.showProgress = false;
- }, 1000);
- }
- },
- handleError(err, response, file) {
- var _file = this.getFile(file);
- var fileList = this.fileList;
- _file.status = 'fail';
- fileList.splice(fileList.indexOf(_file), 1);
- this.onError(err, response, file);
- },
- handleRemove(file) {
- var fileList = this.fileList;
- fileList.splice(fileList.indexOf(file), 1);
- this.onRemove(file, fileList);
- },
- getFile(file) {
- var fileList = this.fileList;
- 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 = [];
- }
- },
- watch: {
- defaultFileList: {
- immediate: true,
- handler(fileList) {
- this.fileList = fileList.map(item => {
- item.status = 'finished';
- item.percentage = 100;
- item.uid = Date.now() + this.tempIndex++;
- return item;
- });
- }
- }
- },
- render(h) {
- var uploadList;
- if (this.showUploadList && !this.thumbnailMode && this.fileList.length) {
- uploadList = (
- <UploadList
- files={this.fileList}
- on-remove={this.handleRemove}
- on-preview={this.handlePreview}>
- </UploadList>
- );
- }
- var props = {
- props: {
- type: this.type,
- action: this.action,
- multiple: this.multiple,
- 'before-upload': this.beforeUpload,
- 'with-credentials': this.withCredentials,
- headers: this.headers,
- name: this.name,
- data: this.data,
- accept: this.thumbnailMode ? 'image/*' : this.accept,
- 'on-start': this.handleStart,
- 'on-progress': this.handleProgress,
- 'on-success': this.handleSuccess,
- 'on-error': this.handleError,
- 'on-preview': this.handlePreview,
- 'on-remove': this.handleRemove
- },
- ref: 'upload-inner'
- };
- var uploadComponent = typeof FormData !== 'undefined'
- ? <upload {...props}>{this.$slots.default}</upload>
- : <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
- if (this.type === 'select') {
- return (
- <div class="el-upload">
- {uploadList}
- {uploadComponent}
- {this.$slots.tip}
- </div>
- );
- }
- if (this.type === 'drag') {
- return (
- <div class="el-upload">
- {uploadComponent}
- {this.$slots.tip}
- {uploadList}
- </div>
- );
- }
- }
- };
- </script>
|