http icon indicating copy to clipboard operation
http copied to clipboard

downloadFile function is not working on Capacitor 3

Open najmulansari opened this issue 4 years ago • 21 comments
trafficstars

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.

najmulansari avatar Jul 03 '21 23:07 najmulansari

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

jlgarcia-vs avatar Jul 04 '21 17:07 jlgarcia-vs

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.

najmulansari avatar Jul 05 '21 02:07 najmulansari

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

thomasvidas avatar Jul 15 '21 14:07 thomasvidas

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

najmulansari avatar Jul 16 '21 22:07 najmulansari

Hi ! The same error here !

baio avatar Jul 25 '21 07:07 baio

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

najmulansari avatar Oct 03 '21 17:10 najmulansari

Have IO Error on android too

elvisgraho avatar Nov 11 '21 08:11 elvisgraho

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

elvisgraho avatar Nov 29 '21 11:11 elvisgraho

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

fmichael avatar Dec 02 '21 19:12 fmichael

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

elvisgraho avatar Dec 13 '21 08:12 elvisgraho

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

ericmoolin avatar Dec 16 '21 00:12 ericmoolin

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.

nikage avatar Dec 21 '21 16:12 nikage

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.

ericmoolin avatar Dec 29 '21 00:12 ericmoolin

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

imclaudiafCC avatar Jan 19 '22 16:01 imclaudiafCC

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 avatar Jan 19 '22 17:01 ericmoolin

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

elvisgraho avatar Jan 21 '22 13:01 elvisgraho

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.

hommalex avatar Apr 04 '22 15:04 hommalex

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 avatar Apr 04 '22 18:04 nikage

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.

nikage avatar Apr 04 '22 18:04 nikage

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.

hommalex avatar Apr 05 '22 11:04 hommalex

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

tylerclark avatar Jun 17 '22 04:06 tylerclark