Save form-data in webapi C #

1

I have the following code in Angular 2 for a post-form method:

    upload(event) {
      let files: FileList = event.target.files;
      let formData = new FormData();
      for (let i = 0, f; f = files[i]; i++) {
        formData.append('attachment', files[i], f.name);
      }
     //call the angular http method
     this.http.
       .post(URL, formData)
       .map((res:Response) =>
          res.json()).subscribe(
             (success) => {
             alert(success._body);
          },
       (error) => alert(error))
      }
   }

It works perfectly by sending everything I like, my problem now is in the creation of the C # API, what kind will I receive in the back compatible with FormData? How do I handle it? Is it possible to save it in the bank? If not, how to save to a folder? Thanks for all the help!

    
asked by anonymous 10.07.2017 / 22:18

1 answer

0

I used this in WebApi in C # and it worked very well. I picked up this information a while ago from the StackOverFlow here: link

public async Task<IHttpActionResult> UploadFile()
{
    if (!Request.Content.IsMimeMultipartContent())
    {
        return StatusCode(HttpStatusCode.UnsupportedMediaType);
    }        

    var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

    foreach (var stream in filesReadToProvider.Contents)
    {
        var fileBytes = await stream.ReadAsByteArrayAsync();
    }
}
    
01.08.2017 / 03:32