Retrieve files and other form data together in the ASP.NET Core Web API (border-based query demarcation)

How would you formulate your parameters for an action method that should receive one file request and one text from the request?

I tried this

 public string Post([FromBody]string name, [FromBody]IFormFile imageFile) 

And tried to hit him from Postman, but he gave me a 500 Internal Server Error . I also cannot debug it, because it never gets into the first statement inside the action method, where I set a breakpoint.

Any idea how we can parse border-based queries and extract files (files) and other text fields? I am new to ASP.NET Core.

+2
c # asp.net-core .net-core
source share
4 answers

I had a similar problem and solved the problem using the [FromForm] attribute and FileUploadModelView in the following function:

 [HttpPost] [Route("upload")] public async Task<IActionResult> Upload([FromForm] FileUploadViewModel model, [FromForm] string member_code) { var file = model.File; ... } 
+3
source share

I use the following code to do this to parse a response from Mailgun that contains both files and text values.

Note that “decryption” is such that property names such as “MessageHeaders” turn into “message headers”; obviously you should use any logic 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<IFormFile> 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<IFormFile>(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; } } } 
+2
source share

This page helped me a lot https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

so now in my code I have a controller method like:

  public async Task<IActionResult> UploadFiles(UploadedFile ups) 

and class for the model as

 public class UploadedFile { public string UploadName { get; set; } public List<IFormFile> Files { get; set; } } 

and the form is

 <form method="post" enctype="multipart/form-data" asp-controller="Files" asp-action="UploadFiles"> 
+1
source share

I assume that you are transferring the file and text in the correct JSON format.

You should create a new class that includes the file and text as properties of the type string. Add a new class to the method arguments with the [FromBody] attribute, and you can parse the image string according to your needs.

Another way would be to access the entire contents of the request through

 await Request.Content.ReadAsStringAsync(); 

Then you have to parse all the content.

0
source share

All Articles