You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.5 KiB

import React from 'react';
import Upload from 'rc-upload';
import assign from 'object-assign';
import UploadList from './uploadList';
import getFileItem from './getFileItem';
const prefixCls = 'ant-upload';
function noop() {
}
// Fix IE file.status problem
// via coping a new Object
function fileToObject(file) {
return {
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate,
name: file.name,
size: file.size,
type: file.type,
9 years ago
uid: file.uid,
response: file.response,
error: file.error
};
}
const AntUpload = React.createClass({
getInitialState() {
return {
fileList: this.props.fileList || this.props.defaultFileList || []
};
},
onStart(file) {
let nextFileList = this.state.fileList.concat();
if (file.length > 0) {
file = file.map(function(f) {
f = fileToObject(f);
f.status = 'uploading';
return f;
});
nextFileList = nextFileList.concat(file);
} else {
file = fileToObject(file);
file.status = 'uploading';
nextFileList.push(file);
}
9 years ago
this.onChange({
9 years ago
file: file,
fileList: nextFileList
9 years ago
});
},
removeFile(file) {
let fileList = this.state.fileList.concat();
let targetItem = getFileItem(file, fileList);
let index = fileList.indexOf(targetItem);
if (index !== -1) {
fileList.splice(index, 1);
9 years ago
return fileList;
}
9 years ago
return null;
},
onSuccess(response, file) {
9 years ago
// 服务器端需要返回标准 json 字符串
// 否则视为失败
try {
if (typeof response === 'string') {
JSON.parse(response);
}
9 years ago
} catch (e) {
this.onError(new Error('No response'), response, file);
return;
}
let fileList = this.state.fileList.concat();
9 years ago
let targetItem = getFileItem(file, fileList);
9 years ago
// 之前已经删除
if (targetItem) {
targetItem.status = 'done';
targetItem.response = response;
this.onChange({
file: targetItem,
fileList: this.state.fileList
});
}
},
onProgress(e, file) {
9 years ago
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
if (targetItem) {
this.onChange({
event: e,
file: file,
fileList: this.state.fileList
});
}
},
onError(error, response, file) {
9 years ago
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
targetItem.error = error;
targetItem.response = response;
targetItem.status = 'error';
this.handleRemove(targetItem);
},
handleRemove(file) {
let fileList = this.removeFile(file);
9 years ago