ahmadtri26

prodcontro

Nov 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { NavParams,NavController, ActionSheetController, ToastController, Platform, LoadingController, Loading } from 'ionic-angular';
  3. import { Validators,FormBuilder,FormGroup} from '@angular/forms';
  4. import { File } from '@ionic-native/file';
  5. import { Transfer, TransferObject } from '@ionic-native/transfer';
  6. import { FilePath } from '@ionic-native/file-path';
  7. import { Camera } from '@ionic-native/camera';
  8. import { MyApp } from '../../app/app.component';
  9. import { HttpService } from '../../providers/http-service';
  10. /**
  11. * Generated class for the AddProductPage page.
  12. *
  13. * See https://ionicframework.com/docs/components/#navigation for more info on
  14. * Ionic pages and navigation.
  15. */
  16. declare var cordova: any;
  17.  
  18. @Component({
  19. selector: 'page-add-product',
  20. templateUrl: 'add-product.html',
  21. })
  22. export class AddProductPage {
  23.  
  24. image:string = null;;
  25. loading: Loading;
  26. trigger_upload: boolean = false;
  27. public addProduct;
  28. public data;
  29.  
  30. constructor(
  31. public navCtrl: NavController,
  32. public navParams: NavParams,
  33. private camera: Camera,
  34. private transfer: Transfer,
  35. private file: File,
  36. private filePath: FilePath,
  37. public actionSheetCtrl: ActionSheetController,
  38. private http :HttpService,
  39. public toastCtrl: ToastController,
  40. public platform: Platform,
  41. public formBuilder:FormBuilder,
  42. public loadingCtrl: LoadingController
  43. )
  44. {
  45. this.data = {};
  46. this.addProduct = this.formBuilder.group(
  47. {
  48. jenis_barang : ['',Validators.required],
  49. nama_barang: ['',Validators.required],
  50. harga : ['',Validators.required],
  51. ket : ['',Validators.required]
  52.  
  53. });
  54.  
  55. }
  56.  
  57. ionViewDidLoad() {
  58. console.log('ionViewDidLoad AddProductPage');
  59. }
  60.  
  61. pictureFromCamera(){
  62. this.takePhoto(this.camera.PictureSourceType.CAMERA);
  63. }
  64.  
  65. pictureFromGallery(){
  66. this.takePhoto(this.camera.PictureSourceType.PHOTOLIBRARY);
  67. console.log("open galery");
  68. }
  69. takePhoto(sourceType) {
  70. // Create options for the Camera Dialog
  71. let options = {
  72. quality: 100,
  73. sourceType: sourceType,
  74. saveToPhotoAlbum: false,
  75. correctOrientation: true
  76. };
  77.  
  78. // Get the data of an image
  79. this.camera.getPicture(options).then((imagePath) => {
  80. console.log("camera and pictureFromGallery",options,imagePath);
  81. // Special handling for Android library
  82. if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
  83. console.log("galeri android",'android');
  84. this.filePath.resolveNativePath(imagePath)
  85. .then(filePath => {
  86. let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
  87. let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
  88. console.log("get path,ger Name",correctPath,currentName);
  89. this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
  90. });
  91. } else {
  92. let currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
  93. let correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
  94. this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
  95. }
  96. }, (err) => {
  97. this.presentToast('Error while selecting image.');
  98. });
  99. }
  100.  
  101. // Create a new name for the image
  102. createFileName() {
  103. let d = new Date(),
  104. n = d.getTime(),
  105. newFileName = n + ".jpg";
  106. return newFileName;
  107. }
  108.  
  109. // Copy the image to a local folder
  110. copyFileToLocalDir(namePath, currentName, newFileName) {
  111. this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
  112. this.image = newFileName;
  113.  
  114. if(this.image != null) {
  115. this.trigger_upload = true;
  116. }
  117. else {
  118. this.trigger_upload =false;
  119. }
  120.  
  121. }, error => {
  122. this.presentToast('Error while storing file.');
  123. });
  124. }
  125.  
  126. presentToast(text) {
  127. let toast = this.toastCtrl.create({
  128. message: text,
  129. duration: 3000,
  130. position: 'top'
  131. });
  132. toast.present();
  133. }
  134.  
  135. // Always get the accurate path to your apps folder
  136. pathForImage(img) {
  137. if (img === null) {
  138. return '';
  139. } else {
  140. return cordova.file.dataDirectory + img;
  141. }
  142. }
  143.  
  144.  
  145. uploadImage() {
  146. console.log("Run upload Image");
  147. // Destination URL
  148. // var url = "http://192.168.0.113/Latihan/upload_dengan_ci/welcom/upload_image";
  149. let url = "http://192.168.0.113/latihanphp/crud_login/upload_image";
  150. console.log("Run URL", url);
  151. let link = url;
  152. // File for Upload
  153. let targetPath = this.pathForImage(this.image);
  154. console.log("Run Upload", targetPath);
  155. // File name only
  156. let filename = this.image;
  157. console.log("Run name", filename);
  158. let ft = this.transfer.create();
  159. let options = {
  160. fileKey: "file",
  161. fileName: filename,
  162. chunkedMode: false,
  163. mimeType: "multipart/form-data",
  164. params : {'fileName': filename}
  165. };
  166. let submit;
  167. submit = {};
  168.  
  169. submit.jenis_barang = this.addProduct.value.jenis_barang;
  170. submit.nama_barang = this.addProduct.value.nama_barang;
  171. submit.harga = this.addProduct.value.harga;
  172. submit.ket = this.addProduct.value.ket;
  173.  
  174. let postParams = {
  175. "link": link,
  176. "data": submit,
  177. "show_error": 0
  178. };
  179. console.log("Run Option", options);
  180. // const fileTransfer: TransferObject = this.transfer.create();
  181. this.loading = this.loadingCtrl.create({
  182. content: 'Uploading...',
  183. });
  184. console.log("Run load")
  185. this.loading.present();
  186. console.log("Run present")
  187.  
  188. // Use the FileTransfer to upload the image
  189. ft.upload(targetPath, url, options).then((result: any) => {
  190. console.log("upload success ", result);
  191. this.loading.dismissAll();
  192.  
  193. this.presentToast('Image succesful uploaded.');
  194. }, (err) => {
  195. console.log("error happen, ", err);
  196. this.loading.dismissAll();
  197. this.presentToast('Error while uploading file.');
  198. });
  199. }
  200. // uploadImage() {
  201. // console.log("Run upload Image");
  202. // // Destination URL
  203. // // var url = "http://192.168.0.113/Latihan/upload_dengan_ci/welcom/upload_image";
  204. // let url = "http://192.168.0.113/latihanphp/crud_login/upload_image";
  205. // console.log("Run URL", url);
  206. // // File for Upload
  207. // let targetPath = this.pathForImage(this.image);
  208. // console.log("Run Upload", targetPath);
  209. // // File name only
  210. // let filename = this.image;
  211. // console.log("Run name", filename);
  212. // let ft = this.transfer.create();
  213. // let options = {
  214. // fileKey: "file",
  215. // fileName: filename,
  216. // chunkedMode: false,
  217. // mimeType: "multipart/form-data",
  218. // params : {'fileName': filename}
  219. // };
  220. // console.log("Run Option", options);
  221. // // let submit;
  222. // // submit = {};
  223. // // submit.jenis_barang = this.addProduct.jenis_barang;
  224. // // submit.nama_barang = this.addProduct.nama_barang;
  225. // // submit.harga = this.addProduct.harga;
  226. // // submit.ket = this.addProduct.ket;
  227. // // const fileTransfer: TransferObject = this.transfer.create();
  228.  
  229. // this.loading = this.loadingCtrl.create({
  230. // content: 'Uploading...',
  231. // });
  232. // console.log("Run load")
  233. // this.loading.present();
  234. // console.log("Run present")
  235.  
  236. // // Use the FileTransfer to upload the image
  237. // ft.upload(targetPath, url, options).then((result: any) => {
  238. // console.log("upload success ", result);
  239. // this.loading.dismissAll();
  240.  
  241. // this.presentToast('Image succesful uploaded.');
  242. // }, (err) => {
  243. // console.log("error happen, ", err);
  244. // this.loading.dismissAll();
  245. // this.presentToast('Error while uploading file.');
  246. // });
  247. // }
  248. // openMenu()
  249. // {
  250. // // MyApp.consoleLog("add product header",this.addProduct.value.jenis_barang);
  251. // let loading = this.loading = this.loadingCtrl.create({
  252. // content: 'Uploading...',
  253. // });
  254.  
  255. // let submit;
  256. // submit = {};
  257. // submit.jenis_barang = this.addProduct.value.jenis_barang;
  258. // submit.nama_barang = this.addProduct.value.nama_barang;
  259. // submit.harga = this.addProduct.value.harga;
  260. // submit.ket = this.addProduct.value.ket;
  261.  
  262. // const fileTransfer: TransferObject = this.transfer.create();
  263.  
  264. // let link = "http://192.168.0.113/latihanphp/crud_login/test_input";
  265.  
  266. // let postParams ={
  267. // "link": link,
  268. // "data": submit,
  269. // "show_error": 0
  270. // };
  271. // this.http.postService(postParams).then((data)=>{
  272. // loading.dismiss();
  273. // this.data.response = data;
  274.  
  275. // });
  276.  
  277. // }
  278.  
  279. openMenu(){
  280.  
  281. let loading = this.loading = this.loadingCtrl.create({
  282. content: 'Uploading...',
  283. });
  284. let link = "http://localhost/latihanphp/crud_login/test_input";
  285.  
  286. let submit;
  287. submit = {};
  288.  
  289. submit.jenis_barang = this.addProduct.value.jenis_barang;
  290. submit.nama_barang = this.addProduct.value.nama_barang;
  291. submit.harga = this.addProduct.value.harga;
  292. submit.ket = this.addProduct.value.ket;
  293.  
  294. let postParams = {
  295. "link": link,
  296. "data": submit,
  297. "show_error": 0
  298. };
  299. this.http.postService(postParams).then((data)=>{
  300. loading.dismiss();
  301. this.data.response = data;
  302.  
  303. });
  304. // this.http.postService(postParams).then((data)=>{
  305. // MyApp.consoleLog("value data", data);
  306. // });
  307. }
  308.  
  309. }
Add Comment
Please, Sign In to add comment