diff --git a/modules/ensemble/lib/action/upload_files_action.dart b/modules/ensemble/lib/action/upload_files_action.dart index d80edaf52..298bb60c1 100644 --- a/modules/ensemble/lib/action/upload_files_action.dart +++ b/modules/ensemble/lib/action/upload_files_action.dart @@ -198,9 +198,22 @@ List? _getRawFiles(dynamic rawFiles, DataContext dataContext) { }); return processedFile; } + + if (files.values.length > 0 && files.values.first is List) { + List processedFile = []; + files.forEach((key, value) { + (value as List).forEach((element) { + if (element is Map && element.containsKey('path')) { + element['fieldName'] = key; + processedFile.add(File.fromJson(element)); + } + }); + }); + return processedFile; + } } - if (files is String) { + if (files is String && files.isNotEmpty) { final rawFiles = File.fromString(files); return [rawFiles]; } diff --git a/modules/ensemble/lib/util/upload_utils.dart b/modules/ensemble/lib/util/upload_utils.dart index 0fbdcc6a6..7b43ab988 100644 --- a/modules/ensemble/lib/util/upload_utils.dart +++ b/modules/ensemble/lib/util/upload_utils.dart @@ -60,10 +60,16 @@ class UploadUtils { lookupMimeType(file.path ?? '', headerBytes: file.bytes) ?? 'application/octet-stream'; if (file.bytes != null) { - multipartFile = http.MultipartFile.fromBytes(file.fieldName ?? fieldName, file.bytes!, - filename: file.name, contentType: MediaType.parse(mimeType)); + final mediaType = MediaType.parse(mimeType); + final filename = file.name?.isNotEmpty ?? false + ? file.name + : generateFileName(mediaType); + multipartFile = http.MultipartFile.fromBytes( + file.fieldName ?? fieldName, file.bytes!, + filename: filename, contentType: mediaType); } else if (file.path != null) { - multipartFile = await http.MultipartFile.fromPath(file.fieldName ?? fieldName, file.path!, + multipartFile = await http.MultipartFile.fromPath( + file.fieldName ?? fieldName, file.path!, filename: file.name, contentType: MediaType.parse(mimeType)); } else { debugPrint('Failed to add ${file.name} ${file.ext} ${file.path}'); @@ -83,13 +89,19 @@ class UploadUtils { if (res.statusCode >= 200 && res.statusCode <= 300) { return HttpResponse(res, APIState.success); } else { - throw Exception('uploadFile: Failed to upload files \nserver response:\n${res.body}'); + throw Exception( + 'uploadFile: Failed to upload files \nserver response:\n${res.body}'); } } catch (error) { onError?.call(error); } return null; } + + static generateFileName(MediaType mediaType) { + final timestamp = DateTime.now().millisecondsSinceEpoch; + return '${mediaType.type}_$timestamp.${mediaType.subtype}'; + } } class MultipartRequest extends http.MultipartRequest {