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

前端 未结 7 685
悲&欢浪女
悲&欢浪女 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:00

    I'm using the following code to accomplish this in order to parse a response from Mailgun, which comprises both files and text values.

    Please note that "dashifying" is just so property names like "MessageHeaders" get turned into "message-headers"; obviously you should use whatever logic makes sense for your use case.

    Controller:

    using System;
    using Microsoft.AspNetCore.Mvc;
    using NuGet.Protocol.Core.v3;
    
    namespace Potato
    {
        [Route("api/[controller]")]
        public class MailgunController : Controller
        {
            [HttpPost]
            public IActionResult Post()
            {
                MailgunEmail email = new MailgunEmail(Request);
    
                return Ok(email.ToJson());
            }
        }
    }
    

    Model:

    using System;
    using System.Reflection;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Http;
    
    namespace Potato
    {
        public class MailgunEmail
        {
            public IEnumerable Attachments { get; set; }
    
            public string Recipient { get; set; }
            public string Sender { get; set; }
            public string From { get; set; }
            public string Subject { get; set; }
            public string BodyPlain { get; set; }
            public string StrippedText { get; set; }
            public string StrippedSignature { get; set; }
            public string BodyHtml { get; set; }
            public string StrippedHtml { get; set; }
            public int? AttachmentCount { get; set; }
            public int Timestamp { get; set; }
            public string Token { get; set; }
            public string Signature { get; set; }
            public string MessageHeaders { get; set; }
            public string ContentIdMap { get; set; }
    
            public MailgunEmail(HttpRequest request)
            {
                var form = request.Form;
    
                Attachments = new List(form.Files);
    
                foreach (var prop in typeof(MailgunEmail).GetProperties()) {
                    string propName = Dashify(prop.Name);
                    var curVal = form[propName];
                    if (curVal.Count > 0) {
                        prop.SetValue(this, To(curVal[0], prop.PropertyType), null);
                    }
                }
            }
    
            private object To(IConvertible obj, Type t)
            {
                Type u = Nullable.GetUnderlyingType(t);
    
                if (u != null) {
                    return (obj == null) ? GetDefaultValue(t) : Convert.ChangeType(obj, u);
                } else {
                    return Convert.ChangeType(obj, t);
                }
            }
    
            private object GetDefaultValue(Type t)
            {
                if (t.GetTypeInfo().IsValueType) {
                    return Activator.CreateInstance(t);
                }
                return null;
            }
    
            private string Dashify(string source)
            {
                string result = "";
                var chars = source.ToCharArray();
                for (int i = 0; i < chars.Length; ++i) {
                    var c = chars[i];
                    if (i > 0 && char.IsUpper(c)) {
                        result += '-';
                    }
                    result += char.ToLower(c);
                }
                return result;
            }
        }
    }
    

提交回复
热议问题