Skip to content

Instantly share code, notes, and snippets.

@rosieks
Created May 16, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosieks/c26c64ee1f0eb19efb07e5783909c20b to your computer and use it in GitHub Desktop.
Save rosieks/c26c64ee1f0eb19efb07e5783909c20b to your computer and use it in GitHub Desktop.
ASP.NET Core handling multipart form data request issue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace aspnet_forddata_issue.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpPost(Name = "upload")]
public ActionResult UploadFile([FromForm]IFormFile file)
{
return Ok();
}
[HttpGet]
public async Task<HttpResponseMessage> Test()
{
string host = this.Url.ActionContext.HttpContext.Request.Host.Value;
string protocol = this.Url.ActionContext.HttpContext.Request.Scheme;
string url = this.Url.RouteUrl("upload", null, protocol, host);
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new MultipartFormDataContent(),
};
request.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
request.Content.Headers.ContentDisposition = null;
var response = await client.SendAsync(request);
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment