mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] Uploader: utils ts (#3793)
This commit is contained in:
parent
73cec3ac96
commit
06cf48c4c5
@ -1,15 +1,11 @@
|
||||
import { createNamespace, addUnit } from '../utils';
|
||||
import { toArray, readFile, isOversize } from './utils';
|
||||
import { toArray, readFile, isOversize, isImageDataUrl } from './utils';
|
||||
import Icon from '../icon';
|
||||
import Image from '../image';
|
||||
import ImagePreview from '../image-preview';
|
||||
|
||||
const [createComponent, bem] = createNamespace('uploader');
|
||||
|
||||
function isImageDataUrl(dataUrl) {
|
||||
return dataUrl.indexOf('data:image') === 0;
|
||||
}
|
||||
|
||||
export default createComponent({
|
||||
inheritAttrs: false,
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
export function toArray(item) {
|
||||
export function toArray<T>(item: T | T[]): T[] {
|
||||
if (Array.isArray(item)) {
|
||||
return item;
|
||||
}
|
||||
@ -6,12 +6,12 @@ export function toArray(item) {
|
||||
return [item];
|
||||
}
|
||||
|
||||
export function readFile(file, resultType) {
|
||||
export function readFile(file: File, resultType: string) {
|
||||
return new Promise(resolve => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = event => {
|
||||
resolve(event.target.result);
|
||||
resolve((event.target as FileReader).result);
|
||||
};
|
||||
|
||||
if (resultType === 'dataUrl') {
|
||||
@ -22,6 +22,10 @@ export function readFile(file, resultType) {
|
||||
});
|
||||
}
|
||||
|
||||
export function isOversize(files, maxSize) {
|
||||
export function isOversize(files: File | File[], maxSize: number): boolean {
|
||||
return toArray(files).some(file => file.size > maxSize);
|
||||
}
|
||||
|
||||
export function isImageDataUrl(dataUrl: string): boolean {
|
||||
return dataUrl.indexOf('data:image') === 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user