File Chooser plugin in ionic

Hi Team,

I need a plugin which can choose any file from phone and convert that file to base64 .urgently required such thing please help me out with this if anybody know.

thanks,

have you tried thiss?

Hi, @chandelkushal

Try this:

Add file chooser, file path and base64 native plugins and import in your module file

Now, you add this code in your component file

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { FileChooser } from '@ionic-native/file-chooser';
import { FilePath } from '@ionic-native/file-path';
import { Base64 } from '@ionic-native/base64';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public filePath: FilePath, public fileChooser: FileChooser, public base64: Base64, public streamingMedia: StreamingMedia, public navCtrl: NavController) {

  }

  fileChoose(){
    // choose your file from the device
	this.fileChooser.open().then(uri => {
		alert('uri'+JSON.stringify(uri));
        // get file path
		this.filePath.resolveNativePath(uri)
		.then(file => {
			alert('file'+JSON.stringify(file));
			let filePath: string = file;
			if (filePath) {
                // convert your file in base64 format
				this.base64.encodeFile(filePath)
                .then((base64File: string) => {
					alert('base64File'+JSON.stringify(base64File));
				}, (err) => {
					alert('err'+JSON.stringify(err));
				});
			}
		})
		.catch(err => console.log(err));
	})
	.catch(e => alert('uri'+JSON.stringify(e)));
  }

}

Now, call this function add this code in your templet file

<button ion-button (click)="fileChoose()">Choose file</button>

Thank you

How can we extract only audio from file manager using File Chooser

getAudio() {
      // let filter={"mime": "audio/mp3"}
    this.filechooser.open({ "mime": "audio/mp3" }).then
    (uri => {
    console.log(uri+" THE VALUE OF THE URI")
    this.filepath.resolveNativePath(uri).then(path =>
      {
        console.log("path After the resolved native path"+path)
          this.copyFileToLocalDir(path);
         
      
      })
    // this.copyFileToLocalDir(uri);
    })
    .catch(e => console.log(e));
       
    }

i’ve tried above code , its does what i asked it lists only audio,but the problem is i cant select is …is there is anyother way or any other plugin to perform this duty

i’m trying to add multiple mime type for the file chooser like this.filechooser.open({ “mime”: “application/pdf,image/png” }).then((uri) => console.log(uri)); but i cannot select any pdf or image file . is there a solution for addidng multiple mime type to file chooser

for images you can use imagePicker plugin! and for choosing files describe the file type as “files”


    let options = {
      mime: "files"
    };
    this.fileChooser
      .open(options)

try this
and i dont thinks so you can’t add multiple mime type if you wanna choose everything leave the mime field empty

1 Like