mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-08 01:53:34 +08:00
Fix image url display with non-image extention in Upload (#9936)
now we treat url following below rules: 1. `data:image..` => image 2. `http://xxx.com/xxxx.(webp|svg|png|gif|jpg|jpeg)` => image 3. `http://xxx.com/xxx.zip` other extensions => non-image 4. `data:application..` other minetypes in base64 text => non-image 5. `http://xxx.com/xxx` without any extensions => image close #9835 close #9681 ref #9621
This commit is contained in:
parent
49719b20d8
commit
a9376ef1ca
@ -13,8 +13,24 @@ const previewFile = (file: File, callback: Function) => {
|
|||||||
reader.readAsDataURL(file);
|
reader.readAsDataURL(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const extname = (url: string) => {
|
||||||
|
if (!url) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const temp = url.split('/');
|
||||||
|
const filename = temp[temp.length - 1];
|
||||||
|
return (/\.[^./\\]*$/.exec(filename) || [''])[0];
|
||||||
|
};
|
||||||
|
|
||||||
const isImageUrl = (url: string): boolean => {
|
const isImageUrl = (url: string): boolean => {
|
||||||
return /^data:image\//.test(url) || /\.(webp|svg|png|gif|jpg|jpeg)$/.test(url);
|
if (/^data:image\//.test(url) || /\.(webp|svg|png|gif|jpg|jpeg)$/.test(url)) {
|
||||||
|
return true;
|
||||||
|
} else if (/^data:/.test(url)) { // other file types of base64
|
||||||
|
return false;
|
||||||
|
} else if (extname(url)) { // other file types which have extension
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class UploadList extends React.Component<UploadListProps, any> {
|
export default class UploadList extends React.Component<UploadListProps, any> {
|
||||||
@ -81,14 +97,9 @@ export default class UploadList extends React.Component<UploadListProps, any> {
|
|||||||
} else if (!file.thumbUrl && !file.url) {
|
} else if (!file.thumbUrl && !file.url) {
|
||||||
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
|
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
|
||||||
} else {
|
} else {
|
||||||
let thumbnail = isImageUrl((file.thumbUrl || file.url) as string) ? (
|
let thumbnail = isImageUrl((file.thumbUrl || file.url) as string)
|
||||||
<img src={file.thumbUrl || file.url} alt={file.name} />
|
? <img src={file.thumbUrl || file.url} alt={file.name} />
|
||||||
) : (
|
: <Icon type="file" className={`${prefixCls}-list-item-icon`} />;
|
||||||
<Icon
|
|
||||||
type="file"
|
|
||||||
style={{ fontSize: 48, color: 'rgba(0,0,0,0.5)' }}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
icon = (
|
icon = (
|
||||||
<a
|
<a
|
||||||
className={`${prefixCls}-list-item-thumbnail`}
|
className={`${prefixCls}-list-item-thumbnail`}
|
||||||
|
@ -248,7 +248,7 @@ exports[`Upload List should non-image format file preview 1`] = `
|
|||||||
class="ant-upload-list ant-upload-list-picture"
|
class="ant-upload-list ant-upload-list-picture"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="ant-upload-list-item ant-upload-list-item-removed"
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="ant-upload-list-item-info"
|
class="ant-upload-list-item-info"
|
||||||
@ -261,8 +261,7 @@ exports[`Upload List should non-image format file preview 1`] = `
|
|||||||
target="_blank"
|
target="_blank"
|
||||||
>
|
>
|
||||||
<i
|
<i
|
||||||
class="anticon anticon-file"
|
class="anticon anticon-file ant-upload-list-item-icon"
|
||||||
style="font-size: 48px; color: rgba(0, 0, 0, 0.5);"
|
|
||||||
/>
|
/>
|
||||||
</a>
|
</a>
|
||||||
<a
|
<a
|
||||||
@ -270,9 +269,177 @@ exports[`Upload List should non-image format file preview 1`] = `
|
|||||||
href="https://cdn.xxx.com/aaa.zip"
|
href="https://cdn.xxx.com/aaa.zip"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
title="xxx.png"
|
title="not-image"
|
||||||
>
|
>
|
||||||
xxx.png
|
not-image
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
title="Remove file"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item-info"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-thumbnail"
|
||||||
|
href="https://cdn.xxx.com/aaa"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
alt="image"
|
||||||
|
src="https://cdn.xxx.com/aaa"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-name"
|
||||||
|
href="https://cdn.xxx.com/aaa"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
title="image"
|
||||||
|
>
|
||||||
|
image
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
title="Remove file"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item-info"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-thumbnail"
|
||||||
|
href="https://cdn.xxx.com/aaa.xx"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-file ant-upload-list-item-icon"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-name"
|
||||||
|
href="https://cdn.xxx.com/aaa.xx"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
title="not-image"
|
||||||
|
>
|
||||||
|
not-image
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
title="Remove file"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item-info"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-thumbnail"
|
||||||
|
href="https://cdn.xxx.com/aaa.png/xx.xx"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-file ant-upload-list-item-icon"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-name"
|
||||||
|
href="https://cdn.xxx.com/aaa.png/xx.xx"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
title="not-image"
|
||||||
|
>
|
||||||
|
not-image
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
title="Remove file"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item-info"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-thumbnail"
|
||||||
|
href="https://cdn.xxx.com/xx.xx/aaa.png"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
alt="image"
|
||||||
|
src="https://cdn.xxx.com/xx.xx/aaa.png"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-name"
|
||||||
|
href="https://cdn.xxx.com/xx.xx/aaa.png"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
title="image"
|
||||||
|
>
|
||||||
|
image
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
class="anticon anticon-cross"
|
||||||
|
title="Remove file"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item ant-upload-list-item-done"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="ant-upload-list-item-info"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-thumbnail"
|
||||||
|
href="https://cdn.xxx.com/xx.xx/aaa.png"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
alt="image"
|
||||||
|
src="data:image/png;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
class="ant-upload-list-item-name"
|
||||||
|
href="https://cdn.xxx.com/xx.xx/aaa.png"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
title="image"
|
||||||
|
>
|
||||||
|
image
|
||||||
</a>
|
</a>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
@ -265,12 +265,44 @@ describe('Upload List', () => {
|
|||||||
it('should non-image format file preview', () => {
|
it('should non-image format file preview', () => {
|
||||||
const list = [
|
const list = [
|
||||||
{
|
{
|
||||||
...fileList[0],
|
name: 'not-image',
|
||||||
|
status: 'done',
|
||||||
uid: -3,
|
uid: -3,
|
||||||
url: 'https://cdn.xxx.com/aaa.zip',
|
url: 'https://cdn.xxx.com/aaa.zip',
|
||||||
thumbUrl: 'data:application/zip;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
thumbUrl: 'data:application/zip;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
||||||
originFileObj: new File([], 'aaa.zip'),
|
originFileObj: new File([], 'aaa.zip'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'image',
|
||||||
|
status: 'done',
|
||||||
|
uid: -4,
|
||||||
|
url: 'https://cdn.xxx.com/aaa',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'not-image',
|
||||||
|
status: 'done',
|
||||||
|
uid: -5,
|
||||||
|
url: 'https://cdn.xxx.com/aaa.xx',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'not-image',
|
||||||
|
status: 'done',
|
||||||
|
uid: -6,
|
||||||
|
url: 'https://cdn.xxx.com/aaa.png/xx.xx',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'image',
|
||||||
|
status: 'done',
|
||||||
|
uid: -6,
|
||||||
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'image',
|
||||||
|
status: 'done',
|
||||||
|
uid: -6,
|
||||||
|
url: 'https://cdn.xxx.com/xx.xx/aaa.png',
|
||||||
|
thumbUrl: 'data:image/png;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const wrapper = mount(
|
const wrapper = mount(
|
||||||
|
@ -17,6 +17,7 @@ export interface UploadFile {
|
|||||||
status?: UploadFileStatus;
|
status?: UploadFileStatus;
|
||||||
percent?: number;
|
percent?: number;
|
||||||
thumbUrl?: string;
|
thumbUrl?: string;
|
||||||
|
isNotImage?: boolean;
|
||||||
originFileObj?: File;
|
originFileObj?: File;
|
||||||
response?: any;
|
response?: any;
|
||||||
error?: any;
|
error?: any;
|
||||||
|
@ -232,6 +232,17 @@
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
top: 8px;
|
top: 8px;
|
||||||
left: 8px;
|
left: 8px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.@{upload-item}-icon {
|
||||||
|
color: @disabled-color;
|
||||||
|
font-size: 36px;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
margin-top: -18px;
|
||||||
|
margin-left: -18px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.@{upload-item}-thumbnail img {
|
.@{upload-item}-thumbnail img {
|
||||||
|
Loading…
Reference in New Issue
Block a user