ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient

后端 未结 3 2171
我寻月下人不归
我寻月下人不归 2020-11-27 10:47

I have a WebApi service handling an upload from a simple form, like this one:

    
3条回答
  •  我在风中等你
    2020-11-27 11:26

    Thank you @Michael Tepper for your answer.

    I had to post attachments to MailGun (email provider) and I had to modify it slightly so it would accept my attachments.

    var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(fileName));
    fileContent.Headers.ContentDisposition = 
            new ContentDispositionHeaderValue("form-data") //<- 'form-data' instead of 'attachment'
    {
        Name = "attachment", // <- included line...
        FileName = "Foo.txt",
    };
    multipartFormDataContent.Add(fileContent);
    

    Here for future reference. Thanks.

提交回复
热议问题