Skip to content

Instantly share code, notes, and snippets.

@SvenPam
Created June 14, 2016 14:24
Show Gist options
  • Save SvenPam/c55eef6f80a3374a1ca13666374a7cab to your computer and use it in GitHub Desktop.
Save SvenPam/c55eef6f80a3374a1ca13666374a7cab to your computer and use it in GitHub Desktop.
POST Multiple Images
[HttpPost]
public async Task<JsonResult> MyAction(IEnumerable<HttpPostedFileBase> files)
{
// Do stuff
...
return new JsonResult() { Data = 'Ok' };
}
<form class="js-my-form" action="/my-action" method="post" enctype = "multipart/form-data">
<input type="file" class="js-file-upload" name="files" accept=".pdf,.doc,.docx,.jpg,.jpeg,.gif,.png" multiple>
<input type="submit" value="Submit" />
</form>
$('.js-my-form').submit(function (e) {
e.preventDefault();
// Wrap form in a FormData object see:- https://developer.mozilla.org/en-US/docs/Web/API/FormData
var formData = new FormData($(this)[0]);
$.ajax({
url: '/my-action',
data: formData,
//ContentType: false is important.
contentType: false,
//processType: false is important.
processData: false,
type: 'POST',
success: function () {
console.log('Done!');
}})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment