Issue
I'm using the plug-in Chooser
from @ionic-native/chooser/ngx
: https://ionicframework.com/docs/native/chooser
My aim is to get a PDF file from my Downloads on my phone, and send it to a server in base64
. But my server is getting a corrupted PDF.
I'm using it like this :
openFiles() {
this.chooser
.getFile('application/pdf')
.then((file) => {
console.log('L-119 --> File : ' + JSON.stringify(file));}
But when I read the file in my console, I'm getting this :
My data is empty ! So I can't do anything with this, or maybe I'm missing something?
For example, for a photo, i'm using the camera plugin, like this :
this.camera.getPicture(options).then(
(imageData) => {
console.log('file ' + JSON.stringify(imageData));
And I'm getting this for result :
My server gets a correct file and can open it. Do you know how could I use these informations from the chooser
plugin of Ionic, to send my PDF correctly?
I would like to do it with the Ionic way.
EDIT 1 :
Using Base64 plugin from Ionic
const filePath = file.uri;
this.base64.encodeFile(filePath).then(
(base64File: string) => {
console.log('my base 64 file is ' + base64File);
},
(err) => {
console.log(err);
}
);
My base64 file from Base64
Plugin is still empty.
EDIT 2 :
For Base64, we need to give the path of the file, not the URI. To get the path, I used Ionic File Path
like this :
this.filePath
.resolveNativePath(file.uri)
.then((resultat) => console.log('file path ' + resultat))
.catch((err) => console.log(err));
I receive this : file:///storage/emulated/0/Download/dummy.pdf'
And then with @Najam Us Saqib answer, I use Ionic Base64
with that path, and I get this :
Problem solved now !
Solution
Use base64
Plugin Base64 to convert your File into base64 and send it to server.
Pass your FilePath to that plugin and it will convert your file into base64.
P.S: I recommend you to upload file as it is don't Use base64. it will enhance your Applications performance.
import { Base64 } from '@ionic-native/base64/ngx';
constructor(private base64: Base64) { }
...
let filePath: string = 'file:///...';
this.base64.encodeFile(filePath).then((base64File: string) => {
console.log(base64File);
}, (err) => {
console.log(err);
});
Answered By - Najam Us Saqib
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.