xxxxxxxxxx
onFileSelected(event) {
if(event.target.files.length > 0)
{
this.myForm.patchValue({
fileName: event.target.files[0],
})
}
}
submit(){
const formData = new FormData();
formData.append('file', this.myForm.get('fileName').value);
this.http.post('http://localhost:3000/upload', formData)
.subscribe(res => {
console.log(res);
alert('Uploaded Successfully.');
})
}
xxxxxxxxxx
<button type="button" (click)="fileInput.click()"
style="background-color: lightseagreen;width: 159px;color: black;border-radius: 15px;">Choose
File</button>
<input #fileInput type="file" id="file" (change)="setFilename(fileInput.files)" hidden />
<label>{{ filename }}</label>
setFilename(files) {
this.documentService.setFile("File");
if (files[0]) {
this.filename = files[0].name;
}
}
onSubmit(files) {
if (this.attachmentType == "") {
this.notificationService.showMessage(AlertMessageType.Error, AppConstant.selectAttachmentType);
return;
}
const formData = new FormData();
if (files[0]) {
formData.append(files[0].name, files[0]);
formData.append("memberId", this.memberEnrollmentHeaderId.toString());
formData.append("attachmentType", this.attachmentType);
}
this.documentService.OtherFileUpload(formData).subscribe({
next: (res) => {
this.OECFileProcessModel = res;
this.notificationService.showMessage(AlertMessageType.Success, AppConstant.attachmentUploadSuccess);
this.attachmentDataList = this.Documents;
this.UploadedFiles = [];
this.Documents = [];
this.getAttchmentList();
}, error: (err) => {
this.notificationService.showMessage(AlertMessageType.Error, AppConstant.fileUploadError);
}
});
}
}
xxxxxxxxxx
fileChange(element) {
this.uploadedFiles = element.target.files;
}
upload() {
let formData = new FormData();
for (var i = 0; i < this.uploadedFiles.length; i++) {
formData.append("uploads[]", this.uploadedFiles[i], this.uploadedFiles[i].name);
}
this.http.post('/api/upload', formData)
.subscribe((response) => {
console.log('response received is ', response);
})
}
xxxxxxxxxx
onFileChange(event) {
const reader = new FileReader();
if (event.target.files && event.target.files.length) {
const [file] = event.target.files;
reader.readAsDataURL(file);
reader.onload = () => {
this.data.parentForm.patchValue({
tso: reader.result
});
// need to run CD since file load runs outside of zone
this.cd.markForCheck();
};
}
}
xxxxxxxxxx
upload() {
let formData = new FormData();
for (var i = 0; i < this.uploadedFiles.length; i++) {
formData.append("uploads[]", this.uploadedFiles[i], this.uploadedFiles[i].name);
}
this.http.post('/api/upload', formData)
.subscribe((response) => {
console.log('response received is ', response);
})
}