I'm using jquery form plugin to pass a image to my action method. I can't seem to be able to set the httppostedfilebase in the action method on my controller to the correct type of data.
$('#imageform').submit(function () {
var id = $('#selectedFile').data('id');
//var filename = $('#selectedFile').val(); //.files[0];
//var filename = document.getElementById('selectedFile').files[0];
//var filename = input.files[0];
var filename = $('#selectedFile').prop('files');
var options = {
target: '#output',
enctype: 'multipart/form-data',
beforeSubmit: showRequest,
success: showResponse,
url: '/ManageSpaces/UploadImage',
data: {
id: id,
image: filename[0]
},
type: 'post'
};
$('#imageform').ajaxSubmit(options);
return false;
});
Here is my controller's action method.
[HttpPost]
public ActionResult UploadImage(int id, HttpPostedFileBase image)
{
//do some stuff here
return Json("Saved");
}
html form data
<form id="imageform" enctype="multipart/form-data">
<input type="file" id="selectedFile" name="selectedFile" style="display: none;" data-id='@Model.YogaSpaceId' />
<input type="button" value="Add Photos" class="btn" id="pictureupload" />
</form>