Receive file and other form data together in ASP.NET Core Web API (boundary based request parsing)

前端 未结 7 725
悲&欢浪女
悲&欢浪女 2020-12-30 00:32

How would you form your parameters for the action method which is supposed to receive one file and one text value from the request?

I tried

7条回答
  •  滥情空心
    2020-12-30 01:03

    This is a quick solution for anyone who is facing the same issue:

    You will use ajax to send the following formData

    let formData: FormData;
    formData = new FormData();
    formData.append('imageFile', imageFile);
    formData.append('name', name);
    

    Then you will receive it in your controller like this:

    public string Post(IFormCollection data, IFormFile imageFile)
    

    Then you will access the data as you do normally:

    var name = data["name"];
    

提交回复
热议问题