admin管理员组文章数量:1401233
I have userponent.html and userponent.ts
All examples I found were in html this way <input type="file" />
I don't want to use this style. I have in my html file:
<button type="button" class="btn" (click)="openPicture()" Browse </button>
Below is the function on ts file:
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.
}
I have found an example on stackoverflow with angular/material
but I dont have this module. Is there any other alternative way, without installing any extra package to solve this?
I have user.ponent.html and user.ponent.ts
All examples I found were in html this way <input type="file" />
I don't want to use this style. I have in my html file:
<button type="button" class="btn" (click)="openPicture()" Browse </button>
Below is the function on ts file:
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.
}
I have found an example on stackoverflow with angular/material
but I dont have this module. Is there any other alternative way, without installing any extra package to solve this?
3 Answers
Reset to default 5you can implement like this
<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);
}
}
Because of browser security features, you're actually required to use the input
tag to access the filesystem. What you can do, though, is hide the input
element and access it from the rest of your code and markup.
Take a look at this related answer to get an idea of what you can do. Otherwise, here's a simple example of how it could work:
<input type="file" style="display: none" #file/>
<button type="button" class="btn" (click)="file.click()" Browse </button
You could try good old hidden input in label:
<label class="btn">
Browse <input type="file" style="display: none;" (change)="handleChange($event)">
</label>
handleChange
implementation would have to consume event:
handleChange(event) {
// consume event.target.files[0] which has File type
}
rest about File
can be found on MDN
本文标签: javascriptBrowse file in AngularJSTypeScript on button clickStack Overflow
版权声明:本文标题:javascript - Browse file in AngularJS + TypeScript on button click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744277077a2598465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论