gpt4 book ai didi

javascript - 单击按钮在 AngularJS + TypeScript 中浏览文件

转载 作者:行者123 更新时间:2023-11-28 04:45:38 25 4
gpt4 key购买 nike

我有 user.component.htmluser.component.ts我找到的所有示例都是以这种方式在 html 中 <input type="file" />我不想使用这种风格。我的 html 文件中有:

<button type="button" class="btn" (click)="openPicture()" Browse </button>

下面是ts文件上的函数:

  public openPicture() {
//for testing
console.log('button clicked to open picture');

var picture = browse.the.picture.and.assing.to.this.variable;

//another test, see on console if picture is read
console.log('%c ', 'font-size: 100px; background: {{picture}} no-repeat;');
// Later display picture variable on html and save to database or do anything desired.
}

我找到了一个 example在 stackoverflow 上使用 angular/material但我没有这个模块。有没有其他替代方法,无需安装任何额外的包来解决这个问题?

最佳答案

你可以这样实现

<input type="file" style="display: none" #file (change)="fileChange($event)"/>
<button type="button" class="btn" (click)="file.click()"> Browse </button>

.ts

export class AppComponent {
file: File;

constructor() {

}

fileChange(file) {
this.file = file.target.files[0];
console.log(this.file.name);
}
}

关于javascript - 单击按钮在 AngularJS + TypeScript 中浏览文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47312562/

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