! . . Album Hình . . !

Tìm kiếm Những Gì Bạn Thích !

5 tháng 8, 2019

net core handle file upload from angular

HTML
<button style="margin-bottom:10px;" mat-raised-button color="accent" (click)="fileUpload.click()">
  {{ 'BUYER.UPLOAD_BUTTON' | translate }}
</button>
<input hidden type="file" id="fileToUpload" #fileUpload multiple (change)="handleFileInput($event)"
  accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />

TS
  handleFileInput(event) {
    swal({
      title: "Are you Sure",
      type: "warning",
      text: "You want to Upload?",
      confirmButtonText: "Yes, upload it!?",
      showCancelButton: true
    }).then(result => {
      if (result.value) {
        this.loaderService.display(true);
        if (event.target.files && event.target.files.length > 0) {
          this.fileToUpload = <Array<File>>event.target.files;
          debugger;
          const fileToUploadForm = new FormData();

          if (this.fileToUpload) {
            Array.from(this.fileToUpload).forEach(e => {
              fileToUploadForm.append("files", e);
            });

            this.service.uploadToDatabase(fileToUploadForm).subscribe(
              data => {
                this.loaderService.display(false);
                swal({
                  title: "File Upload with the result as follows:",
                  type: "success",
                  html:
                    '<table cellspacing="15" cellpadding="6" style="display:block;margin-left:auto;margin-right:auto; width:60%">' +
                    '<tr><td style="background-color:#5ab857"><span style="color:white;">' +
                    data[0] +
                    " SUCCESSFULLY INSERTED</span></td></tr>" +
                    '<tr><td style="background-color:#dcb455"><span style="color:white;">' +
                    data[1] +
                    " DUPLICATE RECORD</span></td></tr>" +
                    '<tr><td style="background-color:#ED1C24"><span style="color:white;">' +
                    data[2] +
                    " ERROR IN INSERTING</span><td></tr></table>",
                  confirmButtonText: "OK"
                }).then(result2 => {
                  if (result2.value) {
                    setTimeout(_ => this.applyFilter(), 0);
                  }
                });
              },
              error => {
                this.loaderService.display(false);
                swal(
                  "Error",
                  "Invalid file Format or System Error!<br/> " + error,
                  "error"
                );
                setTimeout(_ => this.applyFilter(), 0);
              }
            );
          }
        }
      }
    });
  }

CS
public async Task<IActionResult> HandleUpload(List<IFormFile> files)
{
foreach (var formFile in files)
{
var filePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Files_Upload", formFile.FileName);
if (formFile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
return Ok(new { count = files.Count });         
}

Không có nhận xét nào:

Hướng dẫn đăng nhận xét của bạn

  • Nếu muốn đăng nhận xét của mình các bạn click vào "Xem và nhận xét ở đây" dưới mỗi bài đăng, sau đó hộp thoại xuất hiện bạn gõ vào những nhận xét của mình. thế là xong! cảm ơn các bạn đã ghé thăm blog của mình !