[英]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.