|
|
@@ -786,6 +786,85 @@ export async function uploadFromChoose(
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+// 新增:自动适应运行环境的文件选择并上传函数
|
|
|
+/**
|
|
|
+ * 自动适应运行环境:选择文件并上传到 MinIO
|
|
|
+ * 小程序:使用 Taro.chooseImage
|
|
|
+ * H5:使用 input[type="file"]
|
|
|
+ */
|
|
|
+export async function uploadFromSelect(
|
|
|
+ uploadPath: string = '',
|
|
|
+ options: {
|
|
|
+ sourceType?: ('album' | 'camera')[],
|
|
|
+ count?: number,
|
|
|
+ accept?: string,
|
|
|
+ maxSize?: number,
|
|
|
+ } = {},
|
|
|
+ callbacks?: MinioProgressCallbacks
|
|
|
+): Promise<UploadResult> {
|
|
|
+ const { sourceType = ['album', 'camera'], count = 1, accept = '*', maxSize = 10 * 1024 * 1024 } = options;
|
|
|
+
|
|
|
+ if (isMiniProgram) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ if (typeof Taro === 'undefined') {
|
|
|
+ reject(new Error('Taro 环境未找到'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Taro.chooseImage({
|
|
|
+ count,
|
|
|
+ sourceType: sourceType as any, // 确保类型兼容
|
|
|
+ success: async (res) => {
|
|
|
+ const tempFilePath = res.tempFilePaths[0];
|
|
|
+ const fileName = res.tempFiles[0]?.name || tempFilePath.split('/').pop() || 'unnamed-file';
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await uploadMinIOWithPolicy(uploadPath, tempFilePath, fileName, callbacks);
|
|
|
+ resolve(result);
|
|
|
+ } catch (error) {
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: reject
|
|
|
+ });
|
|
|
+ });
|
|
|
+ } else if (isBrowser) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const input = document.createElement('input');
|
|
|
+ input.type = 'file';
|
|
|
+ input.accept = accept;
|
|
|
+ input.multiple = count > 1;
|
|
|
+
|
|
|
+ input.onchange = async (event) => {
|
|
|
+ const files = (event.target as HTMLInputElement).files;
|
|
|
+ if (!files || files.length === 0) {
|
|
|
+ reject(new Error('未选择文件'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const file = files[0];
|
|
|
+ if (file.size > maxSize) {
|
|
|
+ reject(new Error(`文件大小超过限制: ${maxSize / 1024 / 1024}MB`));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const fileName = file.name || 'unnamed-file';
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await uploadMinIOWithPolicy(uploadPath, file, fileName, callbacks);
|
|
|
+ resolve(result);
|
|
|
+ } catch (error) {
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ input.click();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ throw new Error('不支持的运行环境');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 默认导出
|
|
|
export default {
|
|
|
MinIOXHRMultipartUploader,
|
|
|
@@ -798,5 +877,6 @@ export default {
|
|
|
getMultipartUploadPolicy,
|
|
|
uploadMinIOWithPolicy,
|
|
|
uploadMinIOWithTaroFile,
|
|
|
- uploadFromChoose
|
|
|
+ uploadFromChoose,
|
|
|
+ uploadFromSelect
|
|
|
};
|