capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: File downloads not working

Open Feuerhamster opened this issue 2 years ago • 2 comments

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.

Feuerhamster avatar Sep 09 '23 23:09 Feuerhamster

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

                        }
                    });

izeryab avatar Oct 03 '23 06:10 izeryab

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

Feuerhamster avatar Oct 06 '23 07:10 Feuerhamster

closing as duplicate of https://github.com/ionic-team/capacitor/issues/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 avatar Feb 27 '24 13:02 jcesarmobile

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[bot] avatar Mar 28 '24 13:03 ionitron-bot[bot]