简体   繁体   English

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

[英]Return File from ngx-image-cropper to upload - Angular

I am using ngx-image-cropper to crop my image.我正在使用ngx-image-cropper裁剪图像。 I need to retrieve the cropped image from ngx-image-cropper to upload.我需要从ngx-image-cropper检索裁剪的图像以上传。 However, I can't retrieve File object through this.但是,我无法通过此检索文件 object。

This is the code that I used to trigger when the user crop the image,这是我用来在用户裁剪图像时触发的代码,

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
}

When I am trying to upload the file it has some error.当我尝试上传文件时,它有一些错误。 So it is better to provide a way to get file object from ngx-image-cropper所以最好提供一种从 ngx-image-cropper 获取文件 object 的方法

The package includes the method base64ToFile package 包含方法base64ToFile

// Import the method:

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

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

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

To return the cropped image as a file instead of a base64 string, add the following:要将裁剪后的图像作为文件而不是 base64 字符串返回,请添加以下内容:

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

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

    console.log(this.data.imageChangedEvent.target.files[0]);
    console.log(this.fileToReturn);
    return this.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 });
  }

I solved sending the original file to the function and creating a new File instance on image-cropper crop( originalFile: File ) function:我解决了将原始文件发送到 function 并在图像裁剪器裁剪( 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; } }

for ^1.5.1 version:对于 ^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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM