fix: multiple files upload bugs

This commit is contained in:
chenos 2020-12-31 14:12:25 +08:00
parent 4fc2843a05
commit f7f8e12ac9
3 changed files with 50 additions and 10 deletions

View File

@ -31,8 +31,8 @@ export default {
}, },
}, },
{ {
interface: 'number', interface: 'attachment',
title: '年龄', title: '头像',
component: { component: {
showInTable: true, showInTable: true,
showInDetail: true, showInDetail: true,

View File

@ -17,6 +17,15 @@ export default {
showInForm: true, showInForm: true,
}, },
}, },
{
interface: 'attachment',
title: '封面',
component: {
showInTable: true,
showInDetail: true,
showInForm: true,
},
},
// { // {
// interface: 'linkTo', // interface: 'linkTo',
// title: '作者', // title: '作者',

View File

@ -107,7 +107,7 @@ export const getImageByUrl = (url, options) => {
function toFileObject(item) { function toFileObject(item) {
console.log(item); console.log(item);
if (typeof item !== 'object') { if (typeof item === 'number') {
return { return {
id: item, id: item,
} }
@ -118,6 +118,7 @@ function toFileObject(item) {
if (item.response && item.response.data) { if (item.response && item.response.data) {
return toFileObject(item.response.data); return toFileObject(item.response.data);
} }
// if (item.org)
return { return {
id: item.id, id: item.id,
uid: `${item.id}`, uid: `${item.id}`,
@ -137,6 +138,39 @@ function toFileList(value: any) {
return value.map(toFileObject); return value.map(toFileObject);
} }
function toValue(item) {
if (typeof item === 'number') {
return {
id: item,
}
}
if (item.id && item.uid && item.url) {
return item;
}
if (item.response && item.response.data) {
return toValue(item.response.data);
}
if (item.originFileObj) {
return item;
}
return {
id: item.id,
uid: `${item.id}`,
name: item.title,
url: item.url,
};
}
function toValues(fileList) {
if (!fileList) {
return [];
}
if (!Array.isArray(fileList) && typeof fileList === 'object') {
fileList = [fileList];
}
return fileList.map(toValue);
}
export const Upload = connect({ export const Upload = connect({
getProps: mapStyledProps getProps: mapStyledProps
})((props) => { })((props) => {
@ -145,13 +179,10 @@ export const Upload = connect({
const uploadProps = { const uploadProps = {
name: 'file', name: 'file',
action: `${process.env.API}/attachments:upload`, action: `${process.env.API}/attachments:upload`,
onChange({ file, fileList }) { onChange({ fileList }) {
if (['done', 'removed'].indexOf(file.status) !== -1) { console.log(fileList);
const values = toFileList(fileList); setFileList((fileList));
console.log(values); onChange(toValues(fileList));
setFileList(values);
onChange(values.map(item => item.id));
}
}, },
}; };
return ( return (