gpt4 book ai didi

angular - 从 FileReader 传递图像以在 Angular 6 中形成输入

转载 作者:太空狗 更新时间:2023-10-29 17:31:55 27 4
gpt4 key购买 nike

我尝试创建一个 UI,其中有一个带有几个文本字段的表单,一个 input type="file"和一个 div您可以拖放图像以与表单的其余部分一起上传。

我的目标/逻辑

使用相同的 div放下图像或单击它并打开文件夹资源管理器,如 input type="file"表现。在几乎不可能“拖放”的小屏幕上启用点击是有意义的。并且由于已经有一个 input type="file"在表格中没有理由从 div 中获取图像并将其附加到表单等。我尝试拍摄放在 div 中的图像。 , 将其设置为 input type="file" 中的值并提交一次表格。 (如果用户点击了 div,那么 input type="file" 已经有一个值,所以我可以再次提交表单)。

这是代码

  <div id="imageDrop" (click)='imageInput.click()' (drop)="drop($event)" (dragover)="allowDrop($event)" #imageDrop>
</div>
<input type="file" formControlName="imageInput" required #imageInput id="imageInput" (change)='imageChange($event)' > <!-- use css to hide it -->

所以,当imageDrop被点击,实际上调用了imageChange通过(click)='imageInput.click()'

这是组件中的 typescript 。

//imageUpload is the name of the reactive form
acceptedImageTypes = {'image/png': true,'image/jpeg': true,'image/gif': true};
@ViewChild('imageDrop') imageDrop;

allowDrop(e) {
e.preventDefault();
}

drop(e) {
e.preventDefault();
//clear in case we selected something before via click
this.imageUpload.controls.imageInput.reset();
this.imageDrop.innerHTML="";
this.checkfiles(e.dataTransfer.files);
}

imageChange(event){
this.imageDrop.innerHTML="";
this.checkfiles(event.target.files);
}//imageChange

checkfiles(files){
if (this.acceptedImageTypes[files[0].type] !== true){
this.imageDrop.nativeElement.innerHTML="Not an image";
return;
}
else if (files.length>1){
this.imageDrop.nativeElement.innerHTML="Only one image/time";
return;
}
else { this.readfiles(files); }
}//checkfiles

readfiles(files){
const reader = new FileReader();
let image = new Image();
reader.onload = (event) =>{
this.imageDrop.nativeElement.innerHTML="";
let fileReader = event.target as FileReader;
image.src = fileReader.result;
image.width = 150;
this.imageDrop.nativeElement.appendChild(image);
};
reader.readAsDataURL(files[0]);

if (this.imageUpload.controls.imageInput.value==null) {
//if its null then means that we dragging an img, so the previous image from the input file is deleted
//now we got to put this image to the input file in order to submit the form
this.imageUpload.controls.imageInput.reset(files[0] );
}
}//readfiles

imageUploadSubmitted(){
//when form submit, for now just check image value to check if its the right one
console.log('IMAGE VALUE SUBMIT = = ',this.imageUpload.controls.imageInput.value);
}

错误

当我尝试拖/放图像时,我得到这个 ERROR DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string指向此 HTML 行 <div id="imageDrop" (click)='imageInput.click()' (drop)="drop($event)" (dragover)="allowDrop($event)" #imageDrop>但我确定它与

if (this.imageUpload.controls.imageInput.value==null) {
this.imageUpload.controls.imageInput.reset(files[0] );
}

readfiles的一部分功能。

有什么办法可以解决这个问题,以便文件输入可以获得一个值,然后可以自由提交表单吗?

谢谢

最佳答案

好的,给你错误的行是 this.imageUpload.controls.imageInput.setValue(files[0]);

原因是,由于安全问题,浏览器会阻止您以编程方式设置文件。

相反,您可以直接使用 e.dataTransfer.files:

let input = this.imageUpload.controls.imageInput as any;
input.files = files;

Here is a fork of your stackblitz

关于angular - 从 FileReader 传递图像以在 Angular 6 中形成输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com