Can you show us some extra details about your situation?
Normally you create a model which represents the form something like:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace YourProject.Models
{
public class YourFormViewModel
{
[Required]
[Display(Name = "Full name")]
public string Name { get; set; }
[Display(Name = "Images")]
public List<HttpPostedFileBase> Images { get; set; }
}
}
Where Images is your upload form element.
Because we are working with an ApiController it could be better to get the upload using:
HttpContext.Current.Request
Then you create your controller something like this:
public class YourController : UmbracoApiController
{
public void DoUpload(YourFormViewModel model)
{
// do something
}
}
Where you have a method which takes the viewmodel.
Then using jQuery you can take the form data and send it with ajax to your controller.
For info about Web Api in Umbraco can be found here:
I tried getting data over to the controller, even put the model to dynamic to just too get some result to debug - but kept getting
"The request entity's media type 'multipart/form-data' is not supported for this resource."
So I referenced the request object directly in the controller (it works, but maybe not "pretty" or best practice)
using System.Web;
using System.Web.Http.Results;
using System.Web.Http;
using Umbraco.Web.WebApi;
namespace SwaggerDemo.Controllers
{
public class FileApiController : UmbracoApiController
{
[HttpPost]
public JsonResult<string> UploadFiles()
{
var files = HttpContext.Current.Request.Files;
var person = HttpContext.Current.Request.Form["person"];
var job = HttpContext.Current.Request.Form["job"];
return Json($"files: {files.Count}");
}
}
}
Ajax file upload to UmbracoApiController
Anyone with a working example of how to upload a file with (jQuery) AJAX to an UmbracoApiController?
Hi Ulf,
Can you show us some extra details about your situation? Normally you create a model which represents the form something like:
Where
Images
is your upload form element.Because we are working with an ApiController it could be better to get the upload using:
Then you create your controller something like this:
Where you have a method which takes the viewmodel.
Then using jQuery you can take the form data and send it with ajax to your controller.
For info about Web Api in Umbraco can be found here:
https://our.umbraco.org/documentation/reference/routing/webapi/
In this Blog post you can also find some more details about Ajax and Umbraco requests:
https://www.wiliam.com.au/wiliam-blog/handling-ajax-postbacks-using-the-umbracoapicontroller
Hope this helps!
/Michaël
Thanks Michaël!
I tried getting data over to the controller, even put the model to dynamic to just too get some result to debug - but kept getting
So I referenced the request object directly in the controller (it works, but maybe not "pretty" or best practice)
Page
Hi Ulf,
You are correct the
HttpPostedFileBase
is better for MVC Controllers, when dealing with ApiControllers you can use your solution.Thanks!
/Michaël
Hi Ulf,
don't forget to mark the answer so others with the same question can find the solution.
Have a nice day!
/Michaël
is working on a reply...