如何在Ionic中将base64字符串转换为JPG或Png [英] How to convert base64 string to JPG or Png in Ionic

查看:69
本文介绍了如何在Ionic中将base64字符串转换为JPG或Png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic App,并且正在使用Ionic中输入的文件执行更新配置文件图像.

I am working in my Ionic App and I am performing the update profile image using the file input in Ionic.

这是我的 updateimage.html :

<ion-content padding style="text-align: center;">
  <ion-grid align-items-center justify-content-center style="height: 100%;">
    <ion-row align-items-center justify-content-center style="height: 100%;">
      <ion-col align-self-center justify-content-center col-12 col-md-6 col-xl-3 style="text-align: center;">
        <h2 class="myformh2">Update Profile Picture</h2>
        <h4 class="myformh2">Image Preview</h4>
        <img src="{{imageUrl}}" class="newimg22" />
        <form [formGroup]="updateprofileimg" (ngSubmit)="changeProfileImage()">
          <ion-list>
            <ion-item class="newitem2">
              <ion-input placeholder="Upload Image" type="file" (change)="onImageSelected($event)"
                [(ngModel)]="img_upload" formControlName="img_upload" required></ion-input>
            </ion-item>
            <div>
              <button [disabled]="!updateprofileimg.valid" ion-button type="submit" class="newbtn11" color="primary"
                block>Change Profile Picture</button>
            </div>
          </ion-list>
        </form>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

在此html中,当用户上传图像时,我正在显示图像的预览.

In this html, when the user upload the image, I am showing the preview of the image.

这是我的 updateimage.ts :

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
import { RestapiProvider } from '../../providers/restapi/restapi';
import { Storage } from '@ionic/storage';

@IonicPage()
@Component({
  selector: 'page-updateimage',
  templateUrl: 'updateimage.html',
})
export class UpdateimagePage {
  updateprofileimg : FormGroup;
  img_upload: any = [];
  selectedImage;
  imageUrl;
  converted_image;
  responseEdit: any;
  constructor(public navCtrl: NavController, public navParams: NavParams,
    private formBuilder: FormBuilder, public restProvider: RestapiProvider,
    private storage: Storage) {
      this.updateprofileimg = this.formBuilder.group({
        img_upload: ['', Validators.required],
      });
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad UpdateimagePage');
  }

  onImageSelected(event) {
    this.selectedImage = event.target.files[0];
    let reader = new FileReader();

    reader.onload = (e: any) => {
      this.imageUrl = e.target.result;
      this.converted_image = "data:image/jpeg;base64,"+this.imageUrl;
    };
    reader.readAsDataURL(this.selectedImage);
  }

  changeProfileImage()
  {
    const fd = new FormData();
    this.storage.get("ID").then((val) =>
    {
      if(val)
      { 
        fd.append('upic', this.selectedImage, this.selectedImage.name);
        this.restProvider.updateprofileimg(fd , 'update_profilepic/'+val).subscribe((data) => {
          if (data) {
            this.responseEdit = data;
            console.log(this.responseEdit.msg);
          }
        });

      }
    });
  }

}

在这个ts文件中,我正在使用API​​将上传的图像发送到后端,但是问题是,它正在发送图像的base64网址.

In this ts file, I am sending the uploaded image to the backend using API but the problem is that, It is sending the base64 url of the image.

非常感谢您的帮助.

推荐答案

只需尝试一下:

  onImageSelected(event) {
    this.selectedImage = event.target.files[0];
    let reader = new FileReader();

    reader.onload = (e: any) => {
      this.imageUrl = e.target.result;
      this.converted_image = "data:image/jpeg;base64,"+this.imageUrl;
    };
    reader.readAsDataURL(this.selectedImage);
  }

  changeProfileImage()
  {
    const fd = new FormData();
    this.storage.get("ID").then((val) =>
    {
      if(val)
      { 
        fd.append('upic', this.selectedImage, this.selectedImage.name);
        fd.append('user_id', val);
        this.restProvider.updateprofileimg(fd , 'update_profilepic/'+val).subscribe((data) => {
          if (data) {
            this.responseEdit = data;
            console.log(this.responseEdit.msg);
          }
        });
      }
    });
  }

这解决了我的问题.

这篇关于如何在Ionic中将base64字符串转换为JPG或Png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆