upload.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:image_picker/image_picker.dart';
  4. import 'package:uuid/uuid.dart';
  5. import 'package:fis_common/logger/logger.dart';
  6. import 'package:universal_html/html.dart' as html;
  7. abstract class UploadUtils {
  8. static XFile? convertBase64ToXFile(String base64Stirng) {
  9. try {
  10. final bytes = base64Decode(base64Stirng);
  11. final tempDir = Directory.systemTemp;
  12. final tempPath = tempDir.path;
  13. final imageId = const Uuid().v4().replaceAll('-', '');
  14. final filePath = '$tempPath/$imageId';
  15. File(filePath).writeAsBytesSync(bytes);
  16. return XFile(filePath);
  17. } catch (e) {
  18. print('Error converting base64 to XFile: $e');
  19. logger.i('Error converting base64 to XFile: $e');
  20. return null;
  21. }
  22. }
  23. static html.File? convertBase64ToFile(String base64Image) {
  24. try {
  25. final bytes = base64Decode(base64Image);
  26. const mimeType = 'image/jpeg'; // 替换为您的图片类型
  27. final blob = html.Blob([bytes], mimeType);
  28. final file = html.File([blob], 'image.jpg'); // 替换为您的文件名
  29. return file;
  30. } catch (e) {
  31. print('Error converting base64 to File: $e');
  32. return null;
  33. }
  34. }
  35. }