Search code examples
typescriptangular7asp.net-core-webapiangular-directiveangular-components

file created with name [object FormData] but it must be Name Delivery.xlsx


I work on asp.net core 2.2 Project with angular 7 . I face issue when download file

It created with Name [object FormData].xlsx but Name must be DeliveryGeneration_Output.xlsx .

on Angular 7

public uploadFile = (files) => {
   
    const formData = new FormData();
    
    formData.append('file', this.fileToUpload,this.fileToUpload.name);
   this.http.post('https://localhost:44396/api/ApprovalQuality/', formData,{ responseType: 'blob' })
      .subscribe((response: Blob) => saveAs(response, formData + '.xlsx'));
         
  }

on Web API .Net Core 2.2

 var memory2 = new MemoryStream();
                    using (var stream = new FileStream(exportPath, FileMode.Open))
                    {
                        stream.CopyTo(memory2);
                    }
                    memory2.Position = 0;
 return File(memory2, "text/plain", Path.GetFileName(exportPath));

Path returned and Created

\\192.168.2.7\\ImportExport\\2\\Export\DeliveryGeneration_Output.xlsx

so why file name created with [object FormData].xlsx and not created with DeliveryGeneration_Output.xlsx

and How to solve this Issue ?


Solution

  • Because the formData is a type of FormData. It adds with .xlsx, will become a string. [object FormData].xlsx

    You need to change it to filename, such as:

    this.http.post('https://localhost:44396/api/ApprovalQuality/', formData,{ responseType: 'blob' })
      .subscribe((response: Blob) => saveAs(response, this.fileToUpload.name + '.xlsx'));   
    }