简体   繁体   English

Angular ngx-img-cropper:如何获取上传图像的文件名?

[英]Angular ngx-img-cropper: How do I get the filename of the uploaded image?

I'm using ngx-img-cropper, https://github.com/web-dave/ngx-img-cropper . 我正在使用ngx-img-cropper, https://github.com/web-dave/ngx-img-cropper How do I get the name of the file I am uploading to my app? 如何获取要上传到我的应用程序中的文件的名称? This is my html code. 这是我的html代码。

<img-cropper #cropper [image]="data" [settings]="cropperSettings"></img-cropper><br>

I have variables named data: any, and cropper: ImageCropperComponent, but ImageCropperComponent doesn't have a filename attribute. 我有一个名为data的变量:any和cropper:ImageCropperComponent,但是ImageCropperComponent没有文件名属性。

If I look at the ngx-img-cropper github page, the src/imageCropperComponent.ts does get the file in the fileChangeListener($event: any) method, but I don't have access to the local file variable inside that method. 如果我查看ngx-img-cropper github页面,则src / imageCropperComponent.ts确实在fileChangeListener($ event:any)方法中获取了文件,但我无法访问该方法中的本地文件变量。

Try this in you component, this should not change the component behaviour. 在您的组件中尝试此操作,这不应更改组件的行为。

...

@ViewChild('cropper') myCopper: ImageCropperComponent;

ngAfterViewInit() {
  if(this.myCopper) {
    const olfOnFileChange = this.myCopper.fileChangeListener.bind(this.myCopper);
    this.myCopper.fileChangeListener = (event) => {
       if ($event.target.files.length === 0) return;
       // here should be your file name
       let fileName: File = $event.target.files[0].name; 
    olfOnFileChange(event);


  }
}

...

There is also the other function setImage, you might want to change that also depending on what you want to do with the file name. 还有另一个函数setImage,您可能还想更改它,具体取决于您要对文件名进行的操作。 Or you can just get it on your component and use it as it is. 或者,您可以将其放在组件上并按原样使用。

This isn't related to the cropper, that's related to how you handle your file upload. 这与裁剪器无关,与您处理文件上传的方式有关。 Here is how you can get the name of the file once uploaded. 上传后,您可以通过以下方法获取文件名。 Of course, adapt it to your case by creating a function in your component and Angular-binding your input to this function ( change event) 当然,通过在组件中创建一个函数并将其输入绑定到该函数( change事件),使其适应您的情况

 displayName = (files) => { const file = files.item(0); console.log('File name is', file.name); } 
 <input type="file" onchange="displayName(this.files)"> 

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

相关问题 在使用ngx-img-cropper的Angular中,如何获取裁剪的jpg或png文件,而不是base64图像 - In Angular using ngx-img-cropper, how do I get the cropped jpg or png file, not a base64 image Angular 9 ngx-image-cropper 使用 slider 调整裁剪器大小 - Angular 9 ngx-image-cropper resize cropper with slider ngx-image-cropper - 如何仅更改裁剪器的长度或宽度 - ngx-image-cropper - how change only the length or width of a cropper 如何结合 ngx-compress-image 和 ngx-image-cropper - how to combine ngx-compress-image and ngx-image-cropper 如何在文件更改事件时从 Angular 中的 ngx-image 裁剪器裁剪后将图像上传到节点 - How to upload image to node after cropping from ngx-image cropper in Angular on change event of file Angular中如何将Heic图像文件转换为Jpeg或Png格式以供ngx-image-cropper使用? - How to convert a Heic image file to a Jpeg or Png in Angular for ngx-image-cropper to use? Angular 8 Ngx-image-cropper 不是 angular 模块 - 导入错误 - Angular 8 Ngx-image-cropper is not an angular module - import error ngx-image-cropper IE-11 不工作 angular 7.2 - ngx-image-cropper IE-11 not working angular 7.2 放大和缩小 Angular 不适用于 ngx-image-cropper - Zoom in & zoom out in Angular not working for ngx-image-cropper 从 ngx-image-cropper 返回文件以上传 - Angular - Return File from ngx-image-cropper to upload - Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM