Skip to content

Instantly share code, notes, and snippets.

@francves
Created May 17, 2018 21:20
Show Gist options
  • Save francves/fcc2ef1a245aa8bfe4f426f9c77c2939 to your computer and use it in GitHub Desktop.
Save francves/fcc2ef1a245aa8bfe4f426f9c77c2939 to your computer and use it in GitHub Desktop.
File upload ionic
<!--
Generated template for the PruebaimagenPage page.
See http://ionicframework.com/docs/components/#navigation for more info on
Ionic pages and navigation.
-->
<ion-header>
<ion-navbar>
<ion-title>pruebaimagen</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-item>
<p>{{imageURI}}</p>
<button ion-button color="secondary" (click)="getImage()">Get Image</button>
</ion-item>
<ion-item>
<h4>Image Preview</h4>
<img src="{{imageFileName}}" *ngIf="imageFileName" alt="Ionic File" width="300" />
</ion-item>
<ion-item>
<button ion-button (click)="uploadFile()">Upload</button>
</ion-item>
</ion-content>
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { LoadingController, ToastController } from 'ionic-angular';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { Camera, CameraOptions } from '@ionic-native/camera';
/**
* Generated class for the PruebaimagenPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-pruebaimagen',
templateUrl: 'pruebaimagen.html',
})
export class PruebaimagenPage {
imageURI:any;
imageFileName:any;
constructor(public navCtrl: NavController,
private transfer: FileTransfer,
private camera: Camera,
public loadingCtrl: LoadingController,
public toastCtrl: ToastController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad PruebaimagenPage');
}
getImage() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
}
this.camera.getPicture(options).then((imageData) => {
this.imageURI = imageData;
this.presentToast(this.imageURI);
}, (err) => {
console.log(err);
this.presentToast(err);
});
}
uploadFile() {
let loader = this.loadingCtrl.create({
content: "Uploading..."
});
loader.present();
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'ionicfile',
fileName: 'ionicfile',
chunkedMode: true,
mimeType: "image/jpeg",
headers: {}
}
fileTransfer.upload(this.imageURI, 'http://192.168.1.100:3000/api/clientemovilimagen', options)
.then((data) => {
console.log(data+" Uploaded Successfully");
this.imageFileName = "http://192.168.0.7:8080/static/images/ionicfile.jpg"
loader.dismiss();
this.presentToast("Image uploaded successfully");
}, (err) => {
console.log(err);
loader.dismiss();
this.presentToast(err);
});
}
presentToast(msg) {
let toast = this.toastCtrl.create({
message: msg,
duration: 3000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment