ant-design/components/upload/index.jsx

246 lines
5.9 KiB
React
Raw Normal View History

2015-08-08 23:37:35 +08:00
import React from 'react';
import Upload from 'rc-upload';
import assign from 'object-assign';
import UploadList from './uploadList';
2015-08-21 16:19:28 +08:00
import getFileItem from './getFileItem';
2015-08-08 23:37:35 +08:00
const prefixCls = 'ant-upload';
2015-08-27 15:46:23 +08:00
function noop() {
}
2015-09-16 18:48:22 +08:00
// Fix IE file.status problem
// via coping a new Object
function fileToObject(file) {
return {
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate,
2015-09-20 13:56:14 +08:00
name: file.filename || file.name,
2015-09-16 18:48:22 +08:00
size: file.size,
type: file.type,
2015-09-16 20:02:29 +08:00
uid: file.uid,
response: file.response,
2015-10-31 13:27:58 +08:00
error: file.error,
percent: 0
};
}
/**
* 生成Progress percent: 0.1 -> 0.98
* - for ie
*/
function genPercentAdd() {
let k = 0.1;
const i = 0.01;
const end = 0.98;
return function(start) {
if (start >= end) {
return start;
} else {
start += k;
k = k - i;
if (k < 0.001) {
k = 0.001;
}
}
return start * 100;
2015-09-16 18:48:22 +08:00
};
}
2015-08-21 16:19:28 +08:00
const AntUpload = React.createClass({
2015-08-08 23:37:35 +08:00
getInitialState() {
return {
2015-11-11 00:28:05 +08:00
fileList: this.props.fileList || this.props.defaultFileList || [],
dragState: 'drop'
2015-08-08 23:37:35 +08:00
};
},
2015-08-27 15:45:38 +08:00
onStart(file) {
2015-09-19 17:05:41 +08:00
let targetItem;
2015-09-09 14:58:17 +08:00
let nextFileList = this.state.fileList.concat();
if (file.length > 0) {
2015-09-19 17:05:41 +08:00
targetItem = file.map(function(f) {
2015-09-16 18:48:22 +08:00
f = fileToObject(f);
2015-09-09 14:58:17 +08:00
f.status = 'uploading';
2015-09-16 18:48:22 +08:00
return f;
2015-09-09 14:58:17 +08:00
});
2015-11-24 17:00:34 +08:00
nextFileList = nextFileList.concat(targetItem);
2015-09-09 14:58:17 +08:00
} else {
2015-09-19 17:05:41 +08:00
targetItem = fileToObject(file);
targetItem.status = 'uploading';
nextFileList.push(targetItem);
2015-09-09 14:58:17 +08:00
}
2015-09-01 11:26:33 +08:00
this.onChange({
2015-09-19 17:05:41 +08:00
file: targetItem,
2015-09-09 14:58:17 +08:00
fileList: nextFileList
2015-08-31 21:26:34 +08:00
});
2015-10-31 13:27:58 +08:00
// fix ie progress
if (!window.FormData) {
this.autoUpdateProgress(0, targetItem);
}
},
autoUpdateProgress(percent, file) {
const getPercent = genPercentAdd();
let curPercent = 0;
this.progressTimer = setInterval(() => {
curPercent = getPercent(curPercent);
this.onProgress({
percent: curPercent
}, file);
}, 200);
2015-08-08 23:37:35 +08:00
},
removeFile(file) {
2015-09-19 17:05:41 +08:00
let fileList = this.state.fileList;
2015-08-31 18:13:16 +08:00
let targetItem = getFileItem(file, fileList);
let index = fileList.indexOf(targetItem);
2015-08-27 17:35:10 +08:00
if (index !== -1) {
2015-08-31 18:13:16 +08:00
fileList.splice(index, 1);
2015-09-01 11:26:33 +08:00
return fileList;
2015-08-27 17:35:10 +08:00
}
2015-09-01 11:26:33 +08:00
return null;
2015-08-27 17:35:10 +08:00
},
onSuccess(response, file) {
2015-10-31 13:27:58 +08:00
this.clearProgressTimer();
2015-09-16 20:02:29 +08:00
// 服务器端需要返回标准 json 字符串
// 否则视为失败
try {
2015-09-18 23:31:07 +08:00
if (typeof response === 'string') {
JSON.parse(response);
}
2015-09-16 20:02:29 +08:00
} catch (e) {
this.onError(new Error('No response'), response, file);
return;
}
2015-09-19 17:05:41 +08:00
let fileList = this.state.fileList;
2015-08-31 21:26:34 +08:00
let targetItem = getFileItem(file, fileList);
2015-09-01 11:26:33 +08:00
// 之前已经删除
if (targetItem) {
targetItem.status = 'done';
targetItem.response = response;
this.onChange({
file: targetItem,
2015-09-19 17:05:41 +08:00
fileList: fileList
2015-09-01 11:26:33 +08:00
});
2015-08-27 16:53:53 +08:00
}
2015-08-08 23:37:35 +08:00
},
2015-08-27 15:45:38 +08:00
onProgress(e, file) {
2015-09-01 11:26:33 +08:00
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
2015-10-31 13:27:58 +08:00
if (!targetItem) return;
targetItem.percent = e.percent;
this.onChange({
event: e,
file: file,
fileList: this.state.fileList
});
2015-08-08 23:37:35 +08:00
},
onError(error, response, file) {
2015-10-31 13:27:58 +08:00
this.clearProgressTimer();
2015-09-16 20:02:29 +08:00
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
targetItem.error = error;
targetItem.response = response;
targetItem.status = 'error';
this.handleRemove(targetItem);
2015-08-27 15:45:38 +08:00
},
handleRemove(file) {
let fileList = this.removeFile(file);
2015-09-01 11:26:33 +08:00
if (fileList) {
this.onChange({
file: file,
fileList: fileList
});
}
2015-08-08 23:37:35 +08:00
},
2015-09-14 00:28:39 +08:00
handleManualRemove(file) {
file.status = 'removed';
this.handleRemove(file);
},
onChange(info) {
2015-10-31 13:27:58 +08:00
this.setState({
fileList: info.fileList
});
2015-09-09 14:58:17 +08:00
this.props.onChange(info);
},
2015-08-08 23:37:35 +08:00
getDefaultProps() {
return {
type: 'select',
name: '',
2015-08-08 23:37:35 +08:00
multipart: false,
action: '',
data: {},
accept: '',
2015-08-31 21:26:34 +08:00
onChange: noop,
2015-11-03 16:58:41 +08:00
showUploadList: true
2015-08-08 23:37:35 +08:00
};
},
componentWillReceiveProps(nextProps) {
if ('fileList' in nextProps) {
this.setState({
fileList: nextProps.fileList
});
}
},
2015-11-11 00:28:05 +08:00
onFileDrop(e) {
2015-11-13 16:09:39 +08:00
this.setState({
dragState: e.type
});
2015-11-11 00:28:05 +08:00
},
2015-10-31 13:27:58 +08:00
clearProgressTimer() {
clearInterval(this.progressTimer);
},
2015-08-08 23:37:35 +08:00
render() {
2015-08-21 16:19:28 +08:00
let type = this.props.type || 'select';
2015-08-27 15:45:38 +08:00
let props = assign({}, this.props, {
onStart: this.onStart,
onError: this.onError,
onProgress: this.onProgress,
onSuccess: this.onSuccess,
});
let uploadList;
2015-11-03 16:58:41 +08:00
if (this.props.showUploadList) {
uploadList = <UploadList items={this.state.fileList} onRemove={this.handleManualRemove} />;
}
if (type === 'drag') {
2015-11-13 16:10:27 +08:00
let dragUploadingClass = this.state.fileList.some(file => file.status === 'uploading')
? `${prefixCls}-drag-uploading` : '';
2015-11-13 16:09:39 +08:00
let draggingClass = this.state.dragState === 'dragover'
? `${prefixCls}-drag-hover` : '';
2015-08-08 23:37:35 +08:00
return (
2015-11-13 16:09:39 +08:00
<span>
<div className={prefixCls + ' ' + prefixCls + '-drag '
+ dragUploadingClass + ' ' + draggingClass}
onDrop={this.onFileDrop}
onDragOver={this.onFileDrop}
onDragLeave={this.onFileDrop}>
2015-10-31 13:27:58 +08:00
<Upload {...props}>
<div className={prefixCls + '-drag-container'}>
{this.props.children}
</div>
</Upload>
</div>
2015-11-13 16:09:39 +08:00
{uploadList}
</span>
2015-08-08 23:37:35 +08:00
);
} else if (type === 'select') {
2015-08-08 23:37:35 +08:00
return (
2015-11-13 16:09:39 +08:00
<span>
<div className={prefixCls + ' ' + prefixCls + '-select'}>
<Upload {...props}>
{this.props.children}
</Upload>
</div>
2015-11-13 16:09:39 +08:00
{uploadList}
</span>
2015-08-08 23:37:35 +08:00
);
}
}
});
AntUpload.Dragger = React.createClass({
render() {
2015-08-27 15:45:38 +08:00
return <AntUpload {...this.props} type="drag" style={{height: this.props.height}}/>;
}
});
export default AntUpload;