我正在使用AngularJS和angular-material编写一个Web应用程序。问题是angular-material没有内置的文件输入组件。(我觉得文件上传不符合Material Design,但是我需要它在我的应用程序中)
你有解决这个问题的好方法吗?
我正在使用AngularJS和angular-material编写一个Web应用程序。问题是angular-material没有内置的文件输入组件。(我觉得文件上传不符合Material Design,但是我需要它在我的应用程序中)
你有解决这个问题的好方法吗?
对于 Angular 6+:
HTML:
<input #csvInput hidden="true" type="file" onclick="this.value=null" (change)="csvInputChange($event)" accept=".csv"/>
<button mat-flat-button color="primary" (click)="csvInput.click()">Choose Spreadsheet File (CSV)</button>
组件方法:
csvInputChange(fileInputEvent: any) {
console.log(fileInputEvent.target.files[0]);
}
注意:此过滤器仅允许.csv
文件。
onclick="this.value=null"
吗? - Mendyleocaseiro的解决方案不错,Github链接
<input class="ng-hide" id="input-file-id" multiple type="file" />
<label for="input-file-id" class="md-button md-raised md-primary">Choose Files</label>
<input type="file">
所做的那样),则需要寻找其他解决方案。 - Guy SchalnatCodePen链接在这里。
<choose-file layout="row">
<input id="fileInput" type="file" class="ng-hide">
<md-input-container flex class="md-block">
<input type="text" ng-model="fileName" disabled>
<div class="hint">Select your file</div>
</md-input-container>
<div>
<md-button id="uploadButton" class="md-fab md-mini">
<md-icon class="material-icons">attach_file</md-icon>
</md-button>
</div>
</choose-file>
.directive('chooseFile', function() {
return {
link: function (scope, elem, attrs) {
var button = elem.find('button');
var input = angular.element(elem[0].querySelector('input#fileInput'));
button.bind('click', function() {
input[0].click();
});
input.bind('change', function(e) {
scope.$apply(function() {
var files = e.target.files;
if (files[0]) {
scope.fileName = files[0].name;
} else {
scope.fileName = null;
}
});
});
}
};
});
希望能对你有所帮助!
根据这个答案。我花了一些时间让这种方法工作,所以我希望我的答案能够节省别人的时间。
指令:
angular.module('app').directive('apsUploadFile', apsUploadFile);
function apsUploadFile() {
var directive = {
restrict: 'E',
templateUrl: 'upload.file.template.html',
link: apsUploadFileLink
};
return directive;
}
function apsUploadFileLink(scope, element, attrs) {
var input = $(element[0].querySelector('#fileInput'));
var button = $(element[0].querySelector('#uploadButton'));
var textInput = $(element[0].querySelector('#textInput'));
if (input.length && button.length && textInput.length) {
button.click(function (e) {
input.click();
});
textInput.click(function (e) {
input.click();
});
}
input.on('change', function (e) {
var files = e.target.files;
if (files[0]) {
scope.fileName = files[0].name;
} else {
scope.fileName = null;
}
scope.$apply();
});
}
上传文件模板.html
<input id="fileInput" type="file" class="ng-hide">
<md-button id="uploadButton"
class="md-raised md-primary"
aria-label="attach_file">
Choose file
</md-button>
<md-input-container md-no-float>
<input id="textInput" ng-model="fileName" type="text" placeholder="No file chosen" ng-readonly="true">
</md-input-container>
来自https://github.com/angular/material/issues/3310的jameswyse
HTML
<input id="fileInput" name="file" type="file" class="ng-hide" multiple>
<md-button id="uploadButton" class="md-raised md-primary"> Choose Files </md-button>
控制器
var link = function (scope, element, attrs) {
const input = element.find('#fileInput');
const button = element.find('#uploadButton');
if (input.length && button.length) {
button.click((e) => input.click());
}
}
对我有用。
使用 Angular Material
HTML
<div (click)="uploadFile.click()">
<button mat-raised-button color="primary">Choose File</button>
<input #uploadFile (change)="upload($event)" type='file' style="display:none"/>
</div>
ts
upload(event:Event){
console.log(event)
}
我参考了一些在这里发布的信息以及使用Angular Material个性化组件的可能性,这是我的贡献,没有外部库,并将所选文件的名称反馈到字段中:
HTML
<mat-form-field class="columns">
<mat-label *ngIf="selectedFiles; else newFile">{{selectedFiles.item(0).name}}</mat-label>
<ng-template #newFile>
<mat-label>Choose file</mat-label>
</ng-template>
<input matInput disabled>
<button mat-icon-button matSuffix (click)="fileInput.click()">
<mat-icon>attach_file</mat-icon>
</button>
<input hidden (change)="selectFile($event)" #fileInput type="file" id="file">
</mat-form-field>
TS
selectFile(event) {
this.selectedFiles = event.target.files;
}
我找到了一个方法来避免样式化自己的选择文件按钮。
因为我正在使用flowjs进行可恢复上传,所以我可以使用来自ng-flow的"flow-btn"指令,它提供了一个带有材料设计风格的选择文件按钮。
请注意,将输入元素包装在 md-button 内将无效。
<label class="btn btn-outline-primary">
<span>Select File</span>
<input type="file">
</label>
input {
display: none;
}
html:
<div class="upload">
<span>upload image</span>
<input
#Image
type="file"
(change)="handleFileInput($event.target.files)"
accept=".jpg,.svg,.png,.jpeg"
/>
<img
width="100%"
height="100%"
*ngIf="imageUrl"
[src]="imageUrl"
class="image"
/>
</div>
app.component.ts
export class AppComponent {
options = [{ value: "This is value 1", checked: true }];
statuses = ["control"];
// name = "Angular";//
fileToUpload: any;
imageUrl: any;
handleFileInput(file: FileList) {
this.fileToUpload = file.item(0);
//Show image preview
let reader = new FileReader();
reader.onload = (event: any) => {
this.imageUrl = event.target.result;
};
reader.readAsDataURL(this.fileToUpload);
}
}
<input type=file>
。将此代码添加到输入容器中。 - govindpatel<input type="file">
确实允许用户选择文件,但按钮只是普通的 HTML 样式,而不是 Material 风格的,这对我来说一点也不好。 - Guy Schalnat