query-awesome/packages/api/query/query-upload/query-upload-browser.ts
2025-05-30 21:36:12 +08:00

52 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { UploadProgress, UploadProgressData } from './core/upload-progress.ts';
import { uploadFileChunked } from './core/upload-chunk.ts';
import { toFile, uploadFiles, randomId } from './query-upload.ts';
export { toFile, randomId };
export { uploadFiles, uploadFileChunked, UploadProgress };
type UploadFileProps = {
onStart?: () => void;
onDone?: () => void;
onProgress?: (progress: number, data: UploadProgressData) => void;
onSuccess?: (res: any) => void;
onError?: (err: any) => void;
token?: string;
};
export type ConvertOpts = {
appKey?: string;
version?: string;
username?: string;
directory?: string;
isPublic?: boolean;
filename?: string;
/**
* 是否不检查应用文件, 默认 true默认不检测
*/
noCheckAppFiles?: boolean;
};
export const uploadChunk = async (file: File, opts: ConvertOpts, props?: UploadFileProps) => {
const uploadProgress = new UploadProgress({
onStart: function () {
props?.onStart?.();
},
onDone: () => {
props?.onDone?.();
},
onProgress: (progress, data) => {
props?.onProgress?.(progress, data!);
},
});
const result = await uploadFileChunked(file, opts, {
uploadProgress,
token: props?.token!,
createEventSource: (url: string, searchParams: URLSearchParams) => {
return new EventSource(url + '?' + searchParams.toString());
},
FormDataFn: FormData,
});
return result;
};