The problem: Is it possible using httpclient to post a HttpPostedFileBase(only available in memory, not on disk.) to another endpoint?
What I've tried:
I have the following controller, we are posting in a file from our frontend and it binds to the file parameter.
public class HomeController : Controller
{
[System.Web.Http.HttpPost]
public async Task<Stream> Index(HttpPostedFileBase file)
{
//file is not null here, everything works as it should.
//Here im preparing a multipart/form-data request to my api endpoint
var fileStreamContent = new StreamContent(file.InputStream);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(fileStreamContent);
var response = await client.PostAsync("http://restapi.dev/api/files/add", formData);
var result = await response.Content.ReadAsStreamAsync();
return result;
}
}
}
I need to pass this request on to another application that's not publicly available(so we can't post directly from the client) That controller looks like this:
[RoutePrefix("api/files")]
public class FilesController : ApiController
{
[HttpPost]
[Route("add")]
public async Task<HttpResponseMessage> Add(HttpPostedFileBase file)
{
//This is the problem, file is always null when I post from my backend.
var files = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
return Request.CreateResponse(HttpStatusCode.OK);
}
}
file is always null, so are files.
What am I missing? When I use postman and post directly to the API endpoint, it works. So im guessing that im doing something wrong in my HomeController?