I use the Flutter_downloader package to download from the API and the file name contains spaces and special characters. For example, "abc ông @.text" will be changed to "abc____.text" when saving the file.
My code:
await FlutterDownloader.enqueue(
url: url,
savedDir: localPath,
saveInPublicStorage: true,
fileName: ""abc ông @.text");
I want to keep its filename as "abc ông @.text"
3
Answers
From the package’s behavior, there is no direct way to change this behavior in the package itself.
One possible workaround is to post-process the downloaded file and rename it to the desired filename. This can be done using the
dart:io
package’s File class, specifically the rename method.So you need to download it, get the path and rename it
Here’s an example of how to do this:
In this code, renameSync changes the name of the file at the given path, which should reflect your desired filename with spaces and special characters.
Please note that you need to handle the case where a file with the same name already exists in the directory. The renameSync method will throw an exception in such a case. If you want to overwrite existing files, you can use renameSync in combination with deleteSync:
This code checks if a file with the desired name already exists, and if it does, deletes it before renaming the downloaded file.
Looking inside the code of the Flutter Downloader package, you can see the following method:
As you can see, they have a method to sanitize the file name and this is why your special characters and spaces are replaced.
Watch is answer maybe this will resolve your issue: https://github.com/fluttercommunity/flutter_downloader/issues/403#issuecomment-956070748