Reputation: 2035
I have HTML page and I call web api to upload file. My client side code is:
<input name="file" type="file" id="load-image" />
<script>
...
var data = new FormData();
var files = $("#load-image").get(0).files;
data.append("UploadedImage", files[0]);
var ajaxRequest = $.ajax({
type: "POST",
url: "/UploadImage",
contentType: false,
processData: false,
data: data
});
And server side code:
public class FileUploadController : ApiController
{
[HttpPost]
public void UploadImage()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())...
It works. Now I would like to do the same in MVC project. I have client side code the same, but server side code is little different.
Base class of controller is Controller(not api controller) and when I try this:
public class FileUploadController : ApiController
[HttpPost]
public void UploadImage()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
I get error that 'System.Web.HttpContextBase' does not contain a definition for 'Current'...
What should I change in MVC that file upload will work the same as in webApi? Client side code is the same?
Upvotes: 0
Views: 219
Reputation: 305
There is a related question here: System.Web.HttpContextBase' does not contain a definition for 'Current' MVC 4 with Elmah Logging
Long story short: Controller has a HttpContext.Current of himself implemented as
public HttpContextBase HttpContext { get; }
therefore u should use:
System.Web.HttpContext.Current
Upvotes: 1