Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

downloadFile function is not working on Capacitor 3 #142

Open
najmulansari opened this issue Jul 3, 2021 · 21 comments
Open

downloadFile function is not working on Capacitor 3 #142

najmulansari opened this issue Jul 3, 2021 · 21 comments

Comments

@najmulansari
Copy link

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
image

I am testing this on Android 10.

Please look into the issue. Thanks for your help.

@jlgarcia-vs
Copy link

As issue #135 (comment) states, just add the method to your downloadFile call.

Example:
method: 'GET|POST|etc'

Regards

@najmulansari
Copy link
Author

najmulansari commented Jul 5, 2021

HI @visiblesoft-org

I tried adding method in options but now getting "IO Error". I am using the below code.

image

Below error, I am getting.

image

Please let me know if I am doing anything wrong.

@thomasvidas
Copy link
Contributor

Can you provide a small github repo that is reproduction of your bug?

@najmulansari
Copy link
Author

Hi @thomasvidas,
Please find a test repo from here
https://github.com/najmulansari/samplebook/tree/master

@baio
Copy link

baio commented Jul 25, 2021

Hi ! The same error here !

@najmulansari
Copy link
Author

Any update on this yet. I am still stuck with this and not able to go ahead with the capacitor upgrade. Please help.

@elvisgraho
Copy link

Have IO Error on android too

@elvisgraho
Copy link

elvisgraho commented Nov 29, 2021

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);
        }
    }

@fmichael
Copy link

fmichael commented Dec 2, 2021

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: "..."
})

@elvisgraho
Copy link

Making dir beforehand doesn't solve IO Error for me. Im on Android 9

@ericmoolin
Copy link

ericmoolin commented Dec 16, 2021

I am also getting the IO error, creating the directory beforehand did not resolve it for me either.

@nikage
Copy link

nikage commented Dec 21, 2021

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.

@ericmoolin
Copy link

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.

@imclaudiafCC
Copy link

Hi everybody!
Is there a follow up to this issue? I'm still getting the IO Error

@ericmoolin
Copy link

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.

@elvisgraho
Copy link

@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.

@hommalex
Copy link

hommalex commented Apr 4, 2022

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.

@nikage
Copy link

nikage commented Apr 4, 2022

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.

@nikage
Copy link

nikage commented Apr 4, 2022

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.

@hommalex
Copy link

hommalex commented Apr 5, 2022

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.

@tylerclark
Copy link

I too have been having issues with downloadFile on Android only. I have opened a separate issue which describes more of my problem.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests