uploadFileFromFilePicker method

Future<File> uploadFileFromFilePicker(
  1. PlatformFile file, {
  2. String uploadUrl = '/upload-file',
})

Uploads a file from a PlatformFile object to the specified upload URL.

Parameters:

  • file: The PlatformFile object representing the file to be uploaded.
  • uploadUrl: The URL to which the file will be uploaded. Defaults to /upload-file.

Returns:

  • A Future that resolves to a File object representing the uploaded file.

Implementation

Future<File> uploadFileFromFilePicker(
  PlatformFile file, {
  String uploadUrl = '/upload-file',
}) async {
  FormData formData = FormData();
  if (kIsWeb && file.bytes != null) {
    formData.files
        .add(MapEntry('file', MultipartFile.fromBytes(file.bytes!)));
  } else {
    formData.files.add(
      MapEntry(
        'file',
        await MultipartFile.fromFile(file.path!),
      ),
    );
  }
  return dio
      .post(uploadUrl, data: formData)
      .then((response) => response.body<File>());
}