我正在尝试从mat-dialog接收文件,它有文件的输入。但也有一些问题。请帮帮我。
父component.ts:
export class TimelineComponent implements OnInit {
cvList = [];
ngOnInit() {
}
addCv() {
const dialogNew = this.dialog.open(NewCvDialogComponent, {
data: {...this.cvList}
});
dialogNew.afterClosed().subscribe(result => {
if (result) {
this.cvList.push(result);
}
});
}
}Mat-对话框component.html:
<div class="dialog">
<h2 mat-dialog-title>Attach CV</h2>
<form fxLayout="column" #form="ngForm">
<input
type="file"
accept=".doc,.docx,.txt,.pdf"
placeholder="Input file"
name="input-file"
[(ngModel)]="data.file"
(change)="addCV($event)"
required
/>
</form>
<div
mat-dialog-actions
fxLayout="row nowrap"
fxLayoutGap="10px"
class="actions"
>
<button
mat-raised-button
color="warn"
[mat-dialog-close]="false"
fxFlex="50"
>
Cancel
</button>
<button
mat-raised-button
color="primary"
[mat-dialog-close]="data"
cdkFocusInitial
fxFlex="50"
[disabled]="form.invalid"
>
Save
</button>
</div>
</div>但是如果我在结果中使用它,我只能得到文件名。我想接收所有的对象与名称,大小等。我可以怎么做呢?
发布于 2018-12-14 01:41:37
解决了。在对话组件中,我只是添加了一个带有输入文件的变量,并使用mat- dialog -close将其传递给父级
发布于 2020-06-15 19:13:09
您将文件路径设置为字符串,对吗?解决方案是将文件更改为字符串,并将数据传递到父组件。
在mat-dialogue.component.ts中添加此文件事件函数
convertTostring(event){
let reader = new FileReader();
if(event.target.files && event.target.files.length) {
const [file] = event.target.files;
reader.readAsDataURL(file);
reader.onload = () => {
this.cvv = reader.result;
};
}
}mat-dialogue.component.html
并将模板更改为mat-dialog-close="data,cvv"
<button
mat-raised-button
color="primary"
[mat-dialog-close]="[data, cvv]"
cdkFocusInitial
fxFlex="50"
[disabled]="form.invalid">
Save
</button>最后,您可以获得parent.component.ts格式的数据
addCv() {
const dialogNew = this.dialog.open(NewCvDialogComponent, {
data: {...this.cvList}
});
dialogNew.afterClosed().subscribe(data => {
if (result) {
console.log(data);
}
});
}https://stackoverflow.com/questions/53659722
复制相似问题