C#: Posting Model from body and file as MultipartFormDataContent

吃可爱长大的小学妹 提交于 2019-12-06 11:32:17

问题


I'm looking for a way to send model and image in one request. I tried to send my model from body, but I don't know how to send the file. Is there another way than sending image and model in a different files?

Here's the POST method from my API:

[HttpPost]
[Route("UploadNewEvent")]
public async Task<IActionResult> CreateNewEventAsync([FromBody] EventModel model)
{
    var file = this.Request.Form.Files.LastOrDefault();

    if (file != null)
    {
        var uploads = Path.Combine(_environment.WebRootPath, "uploads");

        using (var fileStream = new FileStream(Path.Combine(uploads, "test.jpg"), FileMode.Create))
        {
            await file.CopyToAsync(fileStream);
        }
     }

     // do sth with model later    
     return Ok();
}

And here's the code from my App:

public async Task SendNewEvent(EventModel model, MediaFile photo)
{
    var uri = $"{baseUri}api/User/Event/CreateNewEvent";

    if (photo != null)
    {
        var multipartContent = new MultipartFormDataContent();
        multipartContent.Add(new StreamContent(photo.GetStream()), "\"file\"", $"\"{photo.Path}\"");

        var httpClient = new HttpClient();               
        var jsonObject = JsonConvert.SerializeObject(model);
        var stringContent = new StringContent(jsonObject, Encoding.UTF8, "application/json");
        var httpResponseMessage = await httpClient.PostAsync(uri, stringContent);
    }
}

回答1:


For passing Model with File parameter, you need to post data as form-data.

Follow steps below:

  1. Change FromBody to FromForm

        [HttpPost]
    [Route("UploadNewEvent")]
    public async Task<IActionResult> CreateNewEventAsync([FromForm] EventModel model)
    {
        // do sth with model later    
        return Ok();
    }
    
  2. Change client code to send form-data instead of json string

        var uri = $"https://localhost:44339/UploadNewEvent";
        FileStream fileStream = new FileStream(@"filepath\T1.PNG", FileMode.Open);
        var multipartContent = new MultipartFormDataContent();
        multipartContent.Add(new StreamContent(fileStream), "\"file\"", @"filepath\T1.PNG");
    
    
        // EventModel other fields
        multipartContent.Add(new StringContent("2"), "Id");
        multipartContent.Add(new StringContent("Tom"), "Name");
        var httpClient = new HttpClient();
        var httpResponseMessage = httpClient.PostAsync(uri, multipartContent).Result;
    
  3. EventModel

    public class EventModel
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public IFormFile File { get; set; }
    }
    


来源:https://stackoverflow.com/questions/52209852/c-posting-model-from-body-and-file-as-multipartformdatacontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!