0

I am using the file_picker plugin to pick a CSV file in Flutter Web. Although I am able to pick the file it is converting the file into bytes (Uint8List). Is there any way I can get the original CSV file or if I can convert these bytes to CSV maybe I can get the path of the file?

Code:

 void pickCSV() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ['csv']);
    if (result != null) {
      var fileBytes = result.files.first.bytes;
      csfFileName.value = result.files.first.name;
    } else {
      // User canceled the picker
    }
  }
3

1 Answer 1

0

I know it's a bit late but you have a couple of choices and maybe it helps others out aswell.

Both of the choices requires server-side processing, so you will need to read on how to do that.

  1. Get the content of the CSV file send it to the server and make a new file on the server with that content. You can use String.fromCharCodes to read the content, in the web, after you select the file.

  2. Convert the Uint8List into a base64 string, using base64Encode function, send it to the server, process it there.

Alternatively, if you use Firebase Storage you can use putData like so:

final metaData = SettableMetadata(contentType: mimeType);
final task = await _storage.ref().child(cloudPath).putData(fileData, metaData);
/// Get the URL
await task.ref.getDownloadURL()
  • Storing the mimeType ensures proper handling of the file when used after download
  • cloudPath means the location in FirebaseStorage, such as: storageDirectory/filename.extension
  • fileData is the Uint8List you provided
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.