I am currently migrating a service to utilise asp.net web api from mvc. I have this ApiController
[Authorize]
public class UploadController : ApiController
{
private readonly ObjectService service;
private readonly string companyId;
public UploadController()
{
this.companyId = "D49AA22B-3476-4FAC-8BEF-38F53F9378F3";
this.service = new ObjectService(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"], this.companyId);
}
// POST api/upload/5
[HttpPost]
[Route("api/upload")]
public IHttpActionResult StartUpload(UploadModel model)
{
try
{
var id = Guid.NewGuid().ToString();
if (!service.Exists(model.File.FileName))
{
service.Add(id);
var stream = new MemoryStream();
var caller = new AsyncMethodCaller(service.Upload);
model.File.InputStream.CopyTo(stream);
var result = caller.BeginInvoke(id, stream, model.File.FileName, new AsyncCallback(CompleteUpload), caller);
}
else
throw new Exception("This file already exists. If you wish to replace the asset, please edit it.");
return Ok(id);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
public void CompleteUpload(IAsyncResult result)
{
var caller = (AsyncMethodCaller)result.AsyncState;
var id = caller.EndInvoke(result);
//this.service.Remove(id);
}
// GET api/upload/progress/5
[HttpGet]
[Route("api/upload/progress/{id}")]
public IHttpActionResult GetCurrentProgress(string id)
{
try
{
var progress = service.GetStatus(id);
return Ok(progress);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
this.service.Dispose();
base.Dispose(disposing);
}
}
As you can see, if I do a post to api/upload it should start to upload my file. These methods have been converted from an existing mvc Controller (which returned JsonResult instead of IHttpActionResult)
my UploadModel looks like this:
public class UploadModel
{
public HttpPostedFileBase File { get; set; }
public string FileName { get; set; }
}
and my jquery looks like this:
function uploadFile() {
var file = $("#File")[0].files[0];
if (file) {
var fileName = "test/" + $('input[type=file]').val().split('\\').pop();
var data = new FormData();
data.append("File", file);
data.append("FileName", fileName);
$.ajax({
url: "/Api/Upload",
type: 'POST',
data: data,
contentType: false,
processData: false,
success: function (data) {
console.log("uploading");
console.log(data);
createCategory(fileName);
},
error: function () {
displayAlert(".message", "There was a problem when uploading the file!", "danger");
}
});
} else {
createCategory();
}
};
When I run this code, I get the error
POST http://r3plica.localhost:3892/Api/Upload 415 (Unsupported Media Type)
but if I comment out the line contentType: false, and run my script I get a bad request, although my breakpoints get hit, the UploadModel is actually null.
Can anyone suggest a solution?
Cheers,
/r3plica