从 ngx-image-cropper 返回文件上传 - Angular

问题描述 投票:0回答:5

我正在使用 ngx-image-cropper 来裁剪我的图像。我需要从 ngx-image-cropper 检索裁剪后的图像才能上传。但是,我无法通过此检索 File 对象。

这是我在用户裁剪图像时触发的代码,

imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    this.cropperHeight = event.height;
    this.cropperWidth = event.width;
    this.croppedEvent =event.file;//This is how i tried to get file
}

当我尝试上传文件时出现一些错误。所以最好提供一种从 ngx-image-cropper 获取文件对象的方法

angular image file blob node-modules
5个回答
26
投票

套件包含方法

base64ToFile

// Import the method:

import { ImageCroppedEvent, base64ToFile } from 'ngx-image-cropper';

.....
imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    let File = base64ToFile(this.croppedImage);
}

来源:https://github.com/Mawi137/ngx-image-cropper/releases/tag/3.0.0


9
投票

要将裁剪后的图像作为文件而不是 base64 字符串返回,请添加以下内容:


import { ImageCroppedEvent, base64ToFile } from 'ngx-image-cropper';

  imageCropped(event: ImageCroppedEvent) {
    // Preview
    this.croppedImage = event.base64;

    const fileToReturn = this.base64ToFile(
      event.base64,
      this.data.imageChangedEvent.target.files[0].name,
    )

    return fileToReturn;
  }


  base64ToFile(data, filename) {

    const arr = data.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);

    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }

    return new File([u8arr], filename, { type: mime });
  }


0
投票

我解决了将原始文件发送到函数并在 image-cropper 上创建一个新的 File 实例 crop(originalFile: File ) function:

import { ImageCroppedEvent, base64ToFile } from 'ngx-image-cropper';
...
crop(originalFile): File {
   ...
   ...
   output.base64 = this.cropToBase64(cropCanvas);
   if(originalFile){
     const file = new File([base64ToFile(output.base64)], originalFile.name, {lastModified: originalFile.lastModified, type: originalFile.type});
     return file;
   }
}


0
投票

对于 ^1.5.1 版本:

imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    const file_image: File = this.imageChangedEvent.target.files[0]; //The change event from your file input (set to null to reset the cropper)
    const file = new File([event.file], file_image.name, {type: file_image.type});

    console.log("file", file);
}

0
投票
import { ImageCroppedEvent, LoadedImage } from 'ngx-image-cropper';

export class YourComponent {
    imageChangedEvent: any = '';
    croppedImage: any = '';
    
    constructor(
      private sanitizer: DomSanitizer
    ) {
    }

    fileChangeEvent(event: any): void {
        this.imageChangedEvent = event;
    }
    imageCropped(event: ImageCroppedEvent) {
      this.croppedImage = this.sanitizer.bypassSecurityTrustUrl(event.objectUrl);
      // event.blob can be used to upload the cropped image
    }
    imageLoaded(image: LoadedImage) {
        // show cropper
    }
    cropperReady() {
        // cropper ready
    }
    loadImageFailed() {
        // show message
    }
}

来源:https://github.com/Mawi137/ngx-image-cropper/releases/tag/7.0.0

© www.soinside.com 2019 - 2024. All rights reserved.