使用带 angular material 2 的输入类型文件
using input type file with angular material 2
如何使用 angular material 2 FAB 按钮打开浏览输入对话框?这可以通过这种方式在 HTML 中完成。
<button type="button">
<label for="file-input">click</label>
</button>
<input type="file" id="file-input" style="display:none">
我试过对 material 2 使用相同的方法,但它不起作用。
<button md-mini-fab type="button">
<label for="fileToUpload">
<md-icon>add</md-icon>
</label>
</button>
<input id="fileToUpload" type="file" style="display:none;">
还有其他可行的方法吗?谢谢。
您只需为文件输入触发 click
。
<button md-mini-fab type="button" onclick="document.getElementById('fileToUpload').click()">
<label for="fileToUpload"><md-icon>add</md-icon></label>
</button>
<input id="fileToUpload" type="file" style="display:none;">
可以是md-button、md-dialog、md-input的组合。迷你 fab 按钮必须有一个点击事件来触发 md-dialog 组件。
<button md-mini-fab type="button" (click)="openDialog()">
<label for="fileToUpload">
<md-icon>add</md-icon>
</label>
</button>
在 md-dialog 组件中,可以添加 md-input 字段。
<h1 md-dialog-title>Dialog</h1>
<div md-dialog-actions>
<md-input-container>
<input mdInput id="fileToUpload" type="file">
</md-input-container>
<p>
<button md-button (click)="dialogRef.close('Option 1')">Option 1</button>
<button md-button (click)="dialogRef.close('Option 2')">Option 2</button>
</p>
</div>
请参阅此 Plunker 示例以获取 all/specific 详细信息。
这是一个简单的解决方案:
<button (click)="fileInput.click()">
<md-icon>library_add</md-icon>
<span>Select</span>
<input #fileInput type="file" (change)="onFileInput($event)" style="display:none;" />
</button>
试试这个:
<input #file type="file" [hidden]="true" accept="image/*" (change)="upload(file.files)">
<button mat-button #upload (click)="file.click()">Upload file</button>
<mat-button>
来自 https://material.angular.io
我们正在隐藏基本输入按钮并将 material 按钮链接到文件上传。
我不确定你的情况,但在我的 Angular5 应用程序中,我使用这个 HTML 结构在服务器上上传文件:
<label for="uploadPicture" class="upload-file">
<mat-icon>add_a_photo</mat-icon>
</label>
<input type="file"
id="uploadPicture"
class="hidden-input"
accept="image/jpeg, .jpeg, image/png, .png, image/pjpeg, .jpg"
(change)="selectFile($event.target.files[0])">
就我而言,这个解决方案效果很好。无需触发 click
事件,因为当您点击与 input
相关的 <label>
时,它与 input
上的 click
相同。
您可以使用按钮和输入。确保 按钮不包含输入 .
<button (click)="fileInput.click()">Upload</button>
<input #fileInput type="file" (change)="onFileInput($event)" multiple style="display:none;" />
您还可以使用包含输入的标签。
<label>
<mat-icon>add</mat-icon>
<input type="file" (change)="onFileInput($event)" multiple />
</label>
如何使用 angular material 2 FAB 按钮打开浏览输入对话框?这可以通过这种方式在 HTML 中完成。
<button type="button">
<label for="file-input">click</label>
</button>
<input type="file" id="file-input" style="display:none">
我试过对 material 2 使用相同的方法,但它不起作用。
<button md-mini-fab type="button">
<label for="fileToUpload">
<md-icon>add</md-icon>
</label>
</button>
<input id="fileToUpload" type="file" style="display:none;">
还有其他可行的方法吗?谢谢。
您只需为文件输入触发 click
。
<button md-mini-fab type="button" onclick="document.getElementById('fileToUpload').click()">
<label for="fileToUpload"><md-icon>add</md-icon></label>
</button>
<input id="fileToUpload" type="file" style="display:none;">
可以是md-button、md-dialog、md-input的组合。迷你 fab 按钮必须有一个点击事件来触发 md-dialog 组件。
<button md-mini-fab type="button" (click)="openDialog()">
<label for="fileToUpload">
<md-icon>add</md-icon>
</label>
</button>
在 md-dialog 组件中,可以添加 md-input 字段。
<h1 md-dialog-title>Dialog</h1>
<div md-dialog-actions>
<md-input-container>
<input mdInput id="fileToUpload" type="file">
</md-input-container>
<p>
<button md-button (click)="dialogRef.close('Option 1')">Option 1</button>
<button md-button (click)="dialogRef.close('Option 2')">Option 2</button>
</p>
</div>
请参阅此 Plunker 示例以获取 all/specific 详细信息。
这是一个简单的解决方案:
<button (click)="fileInput.click()">
<md-icon>library_add</md-icon>
<span>Select</span>
<input #fileInput type="file" (change)="onFileInput($event)" style="display:none;" />
</button>
试试这个:
<input #file type="file" [hidden]="true" accept="image/*" (change)="upload(file.files)">
<button mat-button #upload (click)="file.click()">Upload file</button>
<mat-button>
来自 https://material.angular.io
我们正在隐藏基本输入按钮并将 material 按钮链接到文件上传。
我不确定你的情况,但在我的 Angular5 应用程序中,我使用这个 HTML 结构在服务器上上传文件:
<label for="uploadPicture" class="upload-file">
<mat-icon>add_a_photo</mat-icon>
</label>
<input type="file"
id="uploadPicture"
class="hidden-input"
accept="image/jpeg, .jpeg, image/png, .png, image/pjpeg, .jpg"
(change)="selectFile($event.target.files[0])">
就我而言,这个解决方案效果很好。无需触发 click
事件,因为当您点击与 input
相关的 <label>
时,它与 input
上的 click
相同。
您可以使用按钮和输入。确保 按钮不包含输入 .
<button (click)="fileInput.click()">Upload</button>
<input #fileInput type="file" (change)="onFileInput($event)" multiple style="display:none;" />
您还可以使用包含输入的标签。
<label>
<mat-icon>add</mat-icon>
<input type="file" (change)="onFileInput($event)" multiple />
</label>