0

I'm trying to send .csv file from my client app (angular 2) to my web api (ASP.NET), and I have done the following:

Tried to make FormData from my .csv file the following way:

public sendData() {

   let formData = new FormData();
   formData.append('file', this.file, this.file.name);
   this.myService.postMyData(formData, this.name)
   .subscribe(data => this.postData = JSON.stringify(data),
              error => this.error = error,
              () => console.log('Sent'));

   }    

Created a service on the client app where I'm sending this .csv file from.

postMyData(formData: any, name: string) {

   this.s = <string><any>name;
   const headers = new Headers();
   headers.append('Content-Disposition', 'form-data');
   const url: string = 'myUrl?methodName=' + name;
   return this.http.post(url, formData, {headers: headers})
                   .map((res: Response) => res.json());

   }

What's the problem now is that I don't know how to get that .csv file on the server. I tried it with the code found below, but I can't get the real content, I can only see the name, content type, length and stuff like that.

 [HttpPost("GetMyCsvFile")]
    public async Task<IActionResult> GetMyCsvFile(string name) {

        var rawMessage = await Request.ReadFormAsync();

        var msg = rawMessage.Files[0];

      ....
    }

And then whatever I do with rawMessage, I can't get the content which I could read and do the stuff needed.

Is this possible to do?

Sonja
  • 316
  • 1
  • 4
  • 13
  • https://stackoverflow.com/questions/17072767/web-api-how-to-access-multipart-form-values-when-using-multipartmemorystreampro/17073113#17073113 – Jota.Toledo Jul 20 '17 at 20:06
  • As far as I noticed, you dont need to set the content headers at all – Jota.Toledo Jul 20 '17 at 20:07
  • Maybe you need to cast the data (FormData) to be able to access it. Take a look at this answer: [https://stackoverflow.com/a/29086631](https://stackoverflow.com/a/29086631) – BogdanC Jul 20 '17 at 20:43
  • @Jota.Toledo thank you for this comment, you're right. But how to know when I need it and when not? Sometimes I can't send a request because I get an exception, and when I set a header then it's working. – Sonja Jul 21 '17 at 08:03
  • In my case using angular 4.3.x and webapi 2, manually setting the header as in https://stackoverflow.com/questions/40214772/file-upload-in-angular-2 caused my controller method to not recognize the content type of the request. Your are using .NET Core webapi, so maybe its different – Jota.Toledo Jul 21 '17 at 08:56

1 Answers1

0

You need to get the file and not the file name. Try this code, I'm getting a CSV file from my angular app.

 public async Task<bool> GetFileFromAngular(IFormFile file) {
        using (var reader = new StreamReader(file.OpenReadStream())) {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture) {
                HasHeaderRecord = true,
                MissingFieldFound = null,
                BadDataFound = null,
                TrimOptions = TrimOptions.Trim
            };

            using (var csv = new CsvReader(reader, config)) {
                try {
                    var records = csv.GetRecords<DrugFormulary>().ToList();
                    var csvProcessor = new CsvProcessor(_dbContext, _configuration);

                    await csvProcessor.ProcessPlan(records);
                } catch (System.Exception ex) {
                    throw ex;
                }
            }
        }
        return true;
    }
JEuvin
  • 866
  • 1
  • 12
  • 31