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

后端 未结 3 2120
我寻月下人不归
我寻月下人不归 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:15

    After much trial and error, here's code that actually works:

    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            var values = new[]
            {
                new KeyValuePair<string, string>("Foo", "Bar"),
                new KeyValuePair<string, string>("More", "Less"),
            };
    
            foreach (var keyValuePair in values)
            {
                content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
            }
    
            var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(fileName));
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "Foo.txt"
            };
            content.Add(fileContent);
    
            var requestUri = "/api/action";
            var result = client.PostAsync(requestUri, content).Result;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 11:20

    You need to look for various subclasses of HttpContent.

    You create a multiform http content and add various parts to it. In your case you have a byte array content and form url encoded along the lines of:

    HttpClient c = new HttpClient();
    var fileContent = new ByteArrayContent(new byte[100]);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                                                {
                                                    FileName = "myFilename.txt"
                                                };
    
    var formData = new FormUrlEncodedContent(new[]
                                                {
                                                    new KeyValuePair<string, string>("name", "ali"),
                                                    new KeyValuePair<string, string>("title", "ostad")
                                                });
    
    
    MultipartContent content = new MultipartContent();
    content.Add(formData);
    content.Add(fileContent);
    c.PostAsync(myUrl, content);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题