3
I’m using ngx-admin with Nebular for a Dashboard project.
My following code works well to send multiple files. However an error occurs when trying to send a folder (dragging the folder to the file input)
What I need to change to make it possible to send a directory?
template-sent.component.html
<input type="file" (change)="upload($event.target.files)" multiple="multiple">
<div class="progress">
<div class="progress-bar" [style.width]="progress + '%'">{{progress}}%</div>
</div>
template-sent.component.ts
import { Component } from "@angular/core";
import {
HttpClient,
HttpEventType
} from "@angular/common/http";
import { map, catchError } from "rxjs/operators";
import { throwError } from "rxjs";
import { environment } from "environments/environment";
@Component({
selector: "template-sent",
templateUrl: "./template-sent.component.html",
styleUrls: ["./template-sent.component.scss"]
})
export class TemplateSentComponent {
progress: number;
constructor(private http: HttpClient) { }
upload(file) {
this.progress = 1;
const formData = new FormData();
for (const index in file) {
if (Object.prototype.hasOwnProperty.call(file, index)) {
formData.append("files[]", file[index]);
}
}
this.http
.post(environment.sentTemplateEmail, formData, {
reportProgress: true,
observe: "events"
})
.pipe(
map((event: any) => {
if (event.type == HttpEventType.UploadProgress) {
this.progress = Math.round((100 / event.total) * event.loaded);
} else if (event.type == HttpEventType.Response) {
this.progress = 100;
console.log(event.body);
}
}),
catchError((err: any) => {
this.progress = null;
alert(err.message);
return throwError(err.message);
})
)
.toPromise();
}
}
Here’s an example of the idea I want to apply (only with an entire directory, and not just with individual files or multiple files)
Very good! Thank you, I was needing this too
– Kamile Pimenta