How to convert the pdf file to base64?

Hi, i’m using the the file chooser plugin and i want to get pdf file and convert it to base64 ? any help please

On the first position why do you even want to convert the pdf to base64??

To upload it to my server in a chat application

I want to let a user to send à file between them

You can do that without converting them to base64.
Did you try the base64 plugin??

yes but it doesn’t work>{
      alert("chooser "+data);
      this.filePath.resolveNativePath(data).then(filePath => {
        alert("filepath "+filePath)
        this.base64.encodeFile(filePath).then(base64File => {
          alert("base64 " + base64File);
        }).catch(err => {
          alert("erreur base64 => " + err);
      }).catch(err => {
        alert("erreur filepath => " +err)
     alert("erreur chooser => " +error);

In that case you have the option to send a pdf file without converting it to base64.

Got it. Thanks, but another question, what about a media file ? E.g: MP3 or mp4
Have a look here.

1 Like

this doesn’t work and this one too>{
      this.file.resolveLocalFilesystemUrl(filePath).then((result) => {
            alert("fileBase64 succeed " + JSON.stringify(fileBase64));
            alert("fileBase64 error " + JSON.stringify(error));
        }).catch((error) => {
           alert(JSON.stringify("error =>  " + error));

1 Like

Hey. Did you manage to convert the pdf file to base64?

For people who still needs to do this i found the alternate workaround by using javascript input type file method
Below are the steps :
in html:

    <input accept="application/pdf"  #fileInput type="file" (change)="onFileChange($event)" />

in TS file :
triggering the input type file click event on a button do this:

@ViewChild(‘fileInput’) fileInputClick: ElementRef;

buttonClick() {;
// this will open file chooser


// here the event gets triggered after selecting the pdf file

onFileChange(event) {

  var filename =[0].name
  console.log("File Name")
  var fileReader = new FileReader();
  fileReader.onload = () => {
   // here this method will return base64 string :D 

The only way that works on ionic 5. Thank you very much