Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component } from '@angular/core';
- import { NavParams,NavController, ActionSheetController, ToastController, Platform, LoadingController, Loading } from 'ionic-angular';
- import { Validators,FormBuilder,FormGroup} from '@angular/forms';
- import { File } from '@ionic-native/file';
- import { Transfer, TransferObject } from '@ionic-native/transfer';
- import { FilePath } from '@ionic-native/file-path';
- import { Camera } from '@ionic-native/camera';
- import { MyApp } from '../../app/app.component';
- import { HttpService } from '../../providers/http-service';
- /**
- * Generated class for the AddProductPage page.
- *
- * See https://ionicframework.com/docs/components/#navigation for more info on
- * Ionic pages and navigation.
- */
- declare var cordova: any;
- @Component({
- selector: 'page-add-product',
- templateUrl: 'add-product.html',
- })
- export class AddProductPage {
- image:string = null;;
- loading: Loading;
- trigger_upload: boolean = false;
- public addProduct;
- public data;
- constructor(
- public navCtrl: NavController,
- public navParams: NavParams,
- private camera: Camera,
- private transfer: Transfer,
- private file: File,
- private filePath: FilePath,
- public actionSheetCtrl: ActionSheetController,
- private http :HttpService,
- public toastCtrl: ToastController,
- public platform: Platform,
- public formBuilder:FormBuilder,
- public loadingCtrl: LoadingController
- )
- {
- this.data = {};
- this.addProduct = this.formBuilder.group(
- {
- jenis_barang : ['',Validators.required],
- nama_barang: ['',Validators.required],
- harga : ['',Validators.required],
- ket : ['',Validators.required]
- });
- }
- ionViewDidLoad() {
- console.log('ionViewDidLoad AddProductPage');
- }
- pictureFromCamera(){
- this.takePhoto(this.camera.PictureSourceType.CAMERA);
- }
- pictureFromGallery(){
- this.takePhoto(this.camera.PictureSourceType.PHOTOLIBRARY);
- console.log("open galery");
- }
- takePhoto(sourceType) {
- // Create options for the Camera Dialog
- let options = {
- quality: 100,
- sourceType: sourceType,
- saveToPhotoAlbum: false,
- correctOrientation: true
- };
- // Get the data of an image
- this.camera.getPicture(options).then((imagePath) => {
- console.log("camera and pictureFromGallery",options,imagePath);
- // Special handling for Android library
- if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
- console.log("galeri android",'android');
- this.filePath.resolveNativePath(imagePath)
- .then(filePath => {
- let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
- let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
- console.log("get path,ger Name",correctPath,currentName);
- this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
- });
- } else {
- let currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
- let correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
- this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
- }
- }, (err) => {
- this.presentToast('Error while selecting image.');
- });
- }
- // Create a new name for the image
- createFileName() {
- let d = new Date(),
- n = d.getTime(),
- newFileName = n + ".jpg";
- return newFileName;
- }
- // Copy the image to a local folder
- copyFileToLocalDir(namePath, currentName, newFileName) {
- this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
- this.image = newFileName;
- if(this.image != null) {
- this.trigger_upload = true;
- }
- else {
- this.trigger_upload =false;
- }
- }, error => {
- this.presentToast('Error while storing file.');
- });
- }
- presentToast(text) {
- let toast = this.toastCtrl.create({
- message: text,
- duration: 3000,
- position: 'top'
- });
- toast.present();
- }
- // Always get the accurate path to your apps folder
- pathForImage(img) {
- if (img === null) {
- return '';
- } else {
- return cordova.file.dataDirectory + img;
- }
- }
- uploadImage() {
- console.log("Run upload Image");
- // Destination URL
- // var url = "http://192.168.0.113/Latihan/upload_dengan_ci/welcom/upload_image";
- let url = "http://192.168.0.113/latihanphp/crud_login/upload_image";
- console.log("Run URL", url);
- let link = url;
- // File for Upload
- let targetPath = this.pathForImage(this.image);
- console.log("Run Upload", targetPath);
- // File name only
- let filename = this.image;
- console.log("Run name", filename);
- let ft = this.transfer.create();
- let options = {
- fileKey: "file",
- fileName: filename,
- chunkedMode: false,
- mimeType: "multipart/form-data",
- params : {'fileName': filename}
- };
- let submit;
- submit = {};
- submit.jenis_barang = this.addProduct.value.jenis_barang;
- submit.nama_barang = this.addProduct.value.nama_barang;
- submit.harga = this.addProduct.value.harga;
- submit.ket = this.addProduct.value.ket;
- let postParams = {
- "link": link,
- "data": submit,
- "show_error": 0
- };
- console.log("Run Option", options);
- // const fileTransfer: TransferObject = this.transfer.create();
- this.loading = this.loadingCtrl.create({
- content: 'Uploading...',
- });
- console.log("Run load")
- this.loading.present();
- console.log("Run present")
- // Use the FileTransfer to upload the image
- ft.upload(targetPath, url, options).then((result: any) => {
- console.log("upload success ", result);
- this.loading.dismissAll();
- this.presentToast('Image succesful uploaded.');
- }, (err) => {
- console.log("error happen, ", err);
- this.loading.dismissAll();
- this.presentToast('Error while uploading file.');
- });
- }
- // uploadImage() {
- // console.log("Run upload Image");
- // // Destination URL
- // // var url = "http://192.168.0.113/Latihan/upload_dengan_ci/welcom/upload_image";
- // let url = "http://192.168.0.113/latihanphp/crud_login/upload_image";
- // console.log("Run URL", url);
- // // File for Upload
- // let targetPath = this.pathForImage(this.image);
- // console.log("Run Upload", targetPath);
- // // File name only
- // let filename = this.image;
- // console.log("Run name", filename);
- // let ft = this.transfer.create();
- // let options = {
- // fileKey: "file",
- // fileName: filename,
- // chunkedMode: false,
- // mimeType: "multipart/form-data",
- // params : {'fileName': filename}
- // };
- // console.log("Run Option", options);
- // // let submit;
- // // submit = {};
- // // submit.jenis_barang = this.addProduct.jenis_barang;
- // // submit.nama_barang = this.addProduct.nama_barang;
- // // submit.harga = this.addProduct.harga;
- // // submit.ket = this.addProduct.ket;
- // // const fileTransfer: TransferObject = this.transfer.create();
- // this.loading = this.loadingCtrl.create({
- // content: 'Uploading...',
- // });
- // console.log("Run load")
- // this.loading.present();
- // console.log("Run present")
- // // Use the FileTransfer to upload the image
- // ft.upload(targetPath, url, options).then((result: any) => {
- // console.log("upload success ", result);
- // this.loading.dismissAll();
- // this.presentToast('Image succesful uploaded.');
- // }, (err) => {
- // console.log("error happen, ", err);
- // this.loading.dismissAll();
- // this.presentToast('Error while uploading file.');
- // });
- // }
- // openMenu()
- // {
- // // MyApp.consoleLog("add product header",this.addProduct.value.jenis_barang);
- // let loading = this.loading = this.loadingCtrl.create({
- // content: 'Uploading...',
- // });
- // let submit;
- // submit = {};
- // submit.jenis_barang = this.addProduct.value.jenis_barang;
- // submit.nama_barang = this.addProduct.value.nama_barang;
- // submit.harga = this.addProduct.value.harga;
- // submit.ket = this.addProduct.value.ket;
- // const fileTransfer: TransferObject = this.transfer.create();
- // let link = "http://192.168.0.113/latihanphp/crud_login/test_input";
- // let postParams ={
- // "link": link,
- // "data": submit,
- // "show_error": 0
- // };
- // this.http.postService(postParams).then((data)=>{
- // loading.dismiss();
- // this.data.response = data;
- // });
- // }
- openMenu(){
- let loading = this.loading = this.loadingCtrl.create({
- content: 'Uploading...',
- });
- let link = "http://localhost/latihanphp/crud_login/test_input";
- let submit;
- submit = {};
- submit.jenis_barang = this.addProduct.value.jenis_barang;
- submit.nama_barang = this.addProduct.value.nama_barang;
- submit.harga = this.addProduct.value.harga;
- submit.ket = this.addProduct.value.ket;
- let postParams = {
- "link": link,
- "data": submit,
- "show_error": 0
- };
- this.http.postService(postParams).then((data)=>{
- loading.dismiss();
- this.data.response = data;
- });
- // this.http.postService(postParams).then((data)=>{
- // MyApp.consoleLog("value data", data);
- // });
- }
- }
Add Comment
Please, Sign In to add comment