0

I have implemented file_picker in my Flutter web app for uploading files. It works fine on Chrome but not on Safari. On Safari, the file picker does not show.

Here is my code:

Future<void> uploadFile(String uploadUrl, {int maxBytes = 2097152}) async {


FilePickerResult? result = await FilePicker.platform.pickFiles(); //type: FileType.image
if (result == null) {
  LogManager().log('No file selected.');
  return;
}

final bytes = result.files.first.bytes;
if (bytes == null) return;

LogManager().log('File size: ${bytes.length}');
if (bytes.length > maxBytes) {
  throw Exception('File size is too large.');
}

final uri = Uri.parse(uploadUrl);

try {
  Map<String, String> headers = {
    'Content-Length': '${bytes.length}',
  };
  await http.put(uri, headers: headers, body: bytes);

} on http.ClientException catch (e) {
  LogManager().log('uploadFile ClientException: $e');

} catch (e) {

  LogManager().log('uploadFile Unhandled exception: $e');

}

LogManager().log('WEB - Image uploaded successfully.');
}

Any ideas on what I am doing wrong ?

5
  • that package not working in Shafari web You can use the HTML file input Commented Dec 26, 2023 at 6:13
  • Not sure what you mean @Parthis. This package is meant to work in Safari web Commented Dec 26, 2023 at 9:08
  • I say file_picker does not work properly in Safari Browser In the meantime, you can use the HTML file input as a temporary workaround. both packages work same Commented Dec 26, 2023 at 9:33
  • You are wrong. The issue was something I was doing wrong not the package which works perfectly fine on Safari Commented Dec 27, 2023 at 9:36
  • ohk man thanks mostly I use HTML file picker Commented Dec 27, 2023 at 9:41

1 Answer 1

1

For anyone who encounters a similar issue. The problem was that I was not launching the file picker directly in the function called by the button tap that launches it. Rather I was doing so in a secondary function which works fine in Chrome but not in Safari. Moving the code:

FilePickerResult? result = await FilePicker.platform.pickFiles();

To the function directly called by the button tap fixed the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

hi, I am facing the same issue. Can you give the whole file picker code that worked for you in safari browser.

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.