Skip to content

Instantly share code, notes, and snippets.

@coolvasanth
Last active April 3, 2020 05:40
Show Gist options
  • Save coolvasanth/b266c5bb382ddbfc60ca1c0f7c9f33c0 to your computer and use it in GitHub Desktop.
Save coolvasanth/b266c5bb382ddbfc60ca1c0f7c9f33c0 to your computer and use it in GitHub Desktop.
choosing .jpg,.pdf,.docs etc files from galley and uploading it to server via your API using IONIC 2. (DOESN'T WORK ON IOS)
// Install file chooser and file transfer API from ionic 2 and import them into your page.ts. and don't forget to add providers in
app.component.ts
import { Component } from '@angular/core';
import { NavController, NavParams} from 'ionic-angular';
import { Home } from '../../homemodule/home';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserProfileService } from '../../services/login.service';
import { ConfirmidentityPage } from '../confirmidentity/confirmidentity';
import { ProgressDialog } from '../../utility/progress-dialog';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { Transfer, FileUploadOptions, TransferObject } from '@ionic-native/transfer';
import { FileChooser } from '@ionic-native/file-chooser';
constructor(public navCtrl: NavController,
public navParams: NavParams,
private builder: FormBuilder,
private userProfileService: UserProfileService,
private progressDialog: ProgressDialog,
private transfer: Transfer,
private camera: Camera,
private fileChooser: FileChooser
) {}
uploadresume()
{
this.fileChooser.open()
.then(uri =>
{
console.log(uri)
const fileTransfer: TransferObject = this.transfer.create();
// regarding detailed description of this you cn just refere ionic 2 transfer plugin in official website
let options1: FileUploadOptions = {
fileKey: 'image_upload_file',
fileName: 'name.pdf',
headers: {},
params: {"app_key":"Testappkey"},
chunkedMode : false
}
fileTransfer.upload(uri, 'your API that can take the required type of file that you want to upload.', options1)
.then((data) => {
// success
alert("success"+JSON.stringify(data));
}, (err) => {
// error
alert("error"+JSON.stringify(err));
});
})
.catch(e => console.log(e));
}
@alabiboo
Copy link

alabiboo commented Apr 1, 2020

Okay! yes, it is chrome inspector that I use. if I select an image file, console.log(base64File); displays a too long string, but if I select a pdf file, nothing is displayed. I try to display the content of base64File before calling the API.

@coolvasanth
Copy link
Author

Okay, you don't have a problem with Base 64 then, all you have is problem with choosing the PDF. Can you please add the code for choosing the PDF ? And sample output after choosing PDF ?

@alabiboo
Copy link

alabiboo commented Apr 2, 2020

Okay,

chooseFileForAndroid() {
this.filechooser
.open()
.then(uri => {
this.filePath
.resolveNativePath(uri)
.then(filePath => {
console.log(filePath);
this.convertToBase64(filePath,false);
})
.catch(() => {
console.log('Error reading path');
});
})
.catch(e => {
console.log(e);
});
}

after choosing the pdf,
console.log(filePath); displayed file:///storage/emulated/0/Download/CSC73010-Assignment-2-S3-2019 (1).pdf

@coolvasanth
Copy link
Author

Hi @alabiboo the same code is working for me. Anyways I tried a alternative for you, kindly have a look at below.

  1. Install File plugin from here > https://ionicframework.com/docs/native/file/
  2. let base64PDF = await this.file.readAsDataURL(pdfFilePath, pdfFilename);

This also worked for me. Let me know your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment