http
http copied to clipboard
downloadFile function is not working on Capacitor 3
Hi There, downloadFile() function is not working on Capacitor 3
It was working fine with Capacitor 2, but it started failing after upgrading to Capacitor 3. Please see the below screenshot.
Screenshots

I am testing this on Android 10.
Please look into the issue. Thanks for your help.
As issue https://github.com/capacitor-community/http/issues/135#issuecomment-868846620 states, just add the method to your downloadFile call.
Example: method: 'GET|POST|etc'
Regards
HI @visiblesoft-org
I tried adding method in options but now getting "IO Error". I am using the below code.

Below error, I am getting.

Please let me know if I am doing anything wrong.
Can you provide a small github repo that is reproduction of your bug?
Hi @thomasvidas, Please find a test repo from here https://github.com/najmulansari/samplebook/tree/master
Hi ! The same error here !
Any update on this yet. I am still stuck with this and not able to go ahead with the capacitor upgrade. Please help.
Have IO Error on android too
const httpOptions: HttpDownloadFileOptions = {
url: normalImageUrlThatWorksOnIOS
fileDirectory: Directory.Cache,
filePath: normalImageNameThatWorksOnIOS,
};
const resp = await Http.downloadFile(httpOptions);
Getting - IO Error
Error jumps to the code line
const storedCall = callbacks.get(result.callbackId);
if (storedCall) {
// looks like we've got a stored call
if (result.error) {
// ensure stacktraces by copying error properties to an Error
result.error = Object.keys(result.error).reduce((err, key) => {
// use any type to avoid importing util and compiling most of .ts files
err[key] = result.error[key];
return err;
}, new cap.Exception(''));
}
if (typeof storedCall.callback === 'function') {
// callback <----- here ist he error line for me
if (result.success) {
storedCall.callback(result.data);
}
else {
storedCall.callback(null, result.error);
}
}
If it helps anyone, I managed to solve the IO error while downloading files on Android using capacitor 3 (that worked fine on iOS).
My specific issue was that I was attempting to download a file to a sub folder without having created the sub folder first. Apparently the http.downloadFile does not have a built in logic or flag to enable recurssive mode, the way Capacitor Filesystem does.
It was erroring out for me in the java side; HttpRequestHandler.java while trying to create the new FileOutputStream().
Example:
Http.downloadFile({
filePath: "test/video.mp4",
fileDirectory: Directory.Data,
method: "GET",
url: "..."
})
this would fail and return IO Error.
Once I added a mkdir before attempting download, it worked fine.
await Filesystem.mkdir({
path: "test",
directory: Directory.Data
})
await Http.downloadFile({
filePath: "test/video.mp4",
fileDirectory: Directory.Data,
method: "GET",
url: "..."
})
Making dir beforehand doesn't solve IO Error for me. Im on Android 9
I am also getting the IO error, creating the directory beforehand did not resolve it for me either.
I've got this error in android logs:
V/Capacitor/Http: Permission 'android.permission.WRITE_EXTERNAL_STORAGE' is granted
E/Capacitor/Plugin: IO Error
java.io.FileNotFoundException: /storage/emulated/0/Download/1640096041184_Calm.ogg: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
at com.getcapacitor.plugin.http.HttpRequestHandler.downloadFile(HttpRequestHandler.java:438)
at com.getcapacitor.plugin.http.Http.downloadFile(Http.java:167)
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592)
at com.getcapacitor.Bridge$$ExternalSyntheticLambda5.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.os.HandlerThread.run(HandlerThread.java:67)
I've figured out that you can use "DOWNLOADS" as forlder on Android 11+ but on android 10 throws the above error.
I am also getting the IO error, creating the directory beforehand did not resolve it for me either.
Just wanted to follow up and say the IO Error I was getting was an interrupt to the IO Stream, which was caused by me trying to run Http.downloadFile() multiple times asynchronously.
Hi everybody! Is there a follow up to this issue? I'm still getting the IO Error
Hi everybody! Is there a follow up to this issue? I'm still getting the IO Error
Try running your application outside of the android studio emulator. I found the error was only present in the emulator.
@ericmoolin im not sure if it is the case. The emulator Android version must match with the live device. It can be the case of - my real device has different Android version and I no longer encounter issues that I have on emulators.
Any news on that. On Android 12: IO message if I use Directory.Downloads Unable to open directory: when I use Directory.Data
It's defo a permission issue.
Anybody would have a solution to open PDF. Browser just downloads the file, it doesn't open it.
Thanks.
I've figured out that you can use "DOWNLOADS" as forlder on Android 11+ but on android 10 throws the above error.
I've used this package https://github.com/agorapulse/capacitor-mediastore for Android 10 and lower as a workaround.
IO message if I use Directory.Downloads
I've figured out that you can use "DOWNLOADS" as folder on Android 11+ but on android 10 throws the above error.
"DOWNLOADS" is not in documentation but I figured out from java source code and it worked for me
Anybody would have a solution to open PDF.
Although it's not quite relevant to this issue, I tried https://ionicframework.com/docs/native/file-opener and it works for Capacitor 3 as well
Browser just downloads the file, it doesn't open it.
it doesn't have to, IMHO.
Thanks nikage for the quick reply. Much appreciate.
"DOWNLOADS" is not in documentation but I figured out from java source code and it worked for me.
Default not working on grade 8 I should mention that I'm debugging with a usb connections to a One Plus Nord9 with ionic cap run android --external. It may to work because of permission, then??? Looks like I have to include a default repertory in grade???
File Opener not working because Capacitor/Cordova Android plugin is using jcenter() And Grade 8 has deprecate it. Here is the message:
× Running Gradle build - failed!
[capacitor] [error] WARNING:: Using flatDirs should be avoided because it doesn't support any meta-data formats.
[capacitor] Currently detected usages:
[capacitor] - repository flatDir used in: project ':app', project ':capacitor-cordova-android-plugins'
[capacitor] WARNING:: Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other Maven repositories.
I have try to replace jcenter() to mavenCentral() but the plugin change it back on build.
Thanks again nikage.
I too have been having issues with downloadFile on Android only. I have opened a separate issue which describes more of my problem.