Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
import 'dart:async';
import 'dart:io';

import 'package:file_picker/file_picker.dart';

abstract class UserFilePicker {
  String? fileName;
  String? description;
  String? mimeType;

  Future<File?> pickSingleUserfile();
}

class UserFilePickerImpl extends UserFilePicker {
  @override
  Future<File?> pickSingleUserfile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      allowMultiple: false,
      type: FileType.custom,
      allowedExtensions: ['jpg', 'pdf', 'doc', 'png', 'jpeg','docx','xls','xlsx'],
    );
    if (result != null && result.files.isNotEmpty) {
      fileName = result.files.first.name;
      description = result.files.first.extension;
      mimeType = getMimeType(result.files.first.extension!);

      return File(result.files.single.path!);
    } else {
      return null;
    }
  }

  String getMimeType(String mimeType) {
    switch (mimeType) {
      case "jpg":
        return "image/jpeg";
      case "pdf":
        return "application/pdf";
      case "doc":
        return "application/msword";
      case "png":
        return "image/png";
      case "jpeg":
        return "image/jpeg";
      case "docx":
        return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
      case "xls":
        return "application/vnd.ms-excel";
      case "xlsx":
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      default:
        return "application/octet-stream";
    }
  }

}
Leave a Comment