Загрузите файл изображения на локальный компьютер, используя ionic 3 - PullRequest
0 голосов
/ 15 мая 2018

Как загрузить файл изображения на локальный компьютер в виде файла json


Я хочу загрузить изображение в файл json, возможно ли загрузить файл изображения в файл json.Если это невозможно, пожалуйста, сообщите мне, как я могу добавить этот файл изображения в папку активов.Кто-нибудь может мне помочь с этим?Я не могу понять, как решить эту проблему.

 import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { Camera, CameraOptions } from '@ionic-native/camera';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs/Observable';

    @Component({
    selector: 'page-home',
    templateUrl: 'home.html'
    })
    export class HomePage {

    base64Image: string;

    constructor(public navCtrl: NavController,private camera: Camera,public 
    http:HttpClient) {

    }

    openCamera(){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum : true
    }
    
    this.camera.getPicture(options).then((imageData) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64:
     this.base64Image = 'data:image/jpeg;base64,' + imageData;
    }, (err) => {
     // Handle error
    }); 
    }

    openGallery(){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum : true,
      sourceType : this.camera.PictureSourceType.PHOTOLIBRARY
    }
    console.log("gallery called....");
    this.camera.getPicture(options).then((imageData) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64:
     this.base64Image = 'data:image/jpeg;base64,' + imageData;
    }, (err) => {
     // Handle error
    }); 
    }

    uploadImage(){
    console.log("upload called....");
    let url = "assets/imgs/data/file.json";
    let postData = new FormData();
    postData.append('file',this.base64Image);
    let data:Observable<any> = this.http.post(url,postData);
    data.subscribe((result) => {
    console.log(result);
    });
    }

    }
<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
 <button ion-button full (click) = "openCamera()">Take Photo</button>
 <button ion-button full (click) = "openGallery()">Open Gallery</button>
 <img [src] = "base64Image"/>
 <button ion-button full (click) = "uploadImage()">Upload Image</button>

</ion-content>

1 Ответ

0 голосов
/ 12 октября 2018

Да.Это возможно.Вы сохраняете изображения в виде строки (base64).

https://ionicframework.com/docs/native/file/

используйте такие методы, как

writeFile (путь, имя файла, текст, параметры)

в вашем файле.

assets / imgs / data / file.json

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...