Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: File downloads not working #6886

Closed
Feuerhamster opened this issue Sep 9, 2023 · 4 comments
Closed

bug: File downloads not working #6886

Feuerhamster opened this issue Sep 9, 2023 · 4 comments

Comments

@Feuerhamster
Copy link

Bug Report

Capacitor Version

Capacitor Doctor

Latest Dependencies:       

  @capacitor/cli: 5.3.0    
  @capacitor/core: 5.3.0   
  @capacitor/android: 5.3.0
  @capacitor/ios: 5.3.0    

Installed Dependencies:    

  @capacitor/ios: not installed    
  @capacitor/cli: 5.3.0
  @capacitor/core: 5.3.0
  @capacitor/android: 5.3.0        

[success] Android looking great! 👌

Platform(s)

  • Android

Current Behavior

If you click on the button that should initialize the download, nothing happens. The android debugger does not show any errors.

Expected Behavior

The user should be prompted to save the file to the downloads folder. Exactly how you would download a file from a website in a browser.

Code Reproduction

import { db } from "$lib/database";
import FileSaver from "file-saver";

async function download() {
  const data = JSON.stringify(await db.table.toArray());
  var blob = new Blob([data], {type: "application/json;charset=utf-8"});
  FileSaver.saveAs(blob, `app-user-data-${Date.now()}.json`);
}

Other Technical Details

npm --version output: 9.6.7

node --version output: v18.17.1

pod --version output (iOS issues only):

Additional Context

What I basically want to achieve is that the user can export the data of the app to a json file so the user can back up data or use it for data analysis. I checked, it is not a problem of the libary FileSaver I am using. The native js way with creating an a element to download a text file is also not working.

@izeryab
Copy link

izeryab commented Oct 3, 2023

Don't think its perfect but somehow i managed to do it like this:

  fetch(jsoncall)
                    .then(res => res.blob()) // Gets the response and returns it as a blob
                    .then(blob => {
                        //saveAs(blob,fileName);
                        //let f = new File([blob],fileName);
                        var reader = new FileReader();//https://developer.mozilla.org/en-US/docs/Web/API/FileReader
                        reader.readAsDataURL(blob);
                        reader.onloadend = function () {
                            var base64data = reader.result;
                                capacitorFilesystem.Filesystem.writeFile({
                                    data: base64data as string,
                                    path: fileName,
                                    directory: capacitorFilesystem.Directory.Data
                                })

                        }
                    });

@Feuerhamster
Copy link
Author

@izeryab But this is still saving files to the Android app data system directory. I want user download not background file saving.

@jcesarmobile
Copy link
Member

closing as duplicate of #5478

You can use Filesystem's downloadFile method for downloading files with more control about were to place the files
https://capacitorjs.com/docs/apis/filesystem#downloadfile

@jcesarmobile jcesarmobile closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2024
Copy link

ionitron-bot bot commented Mar 28, 2024

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Mar 28, 2024
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

4 participants