Created
June 14, 2016 14:24
-
-
Save SvenPam/c55eef6f80a3374a1ca13666374a7cab to your computer and use it in GitHub Desktop.
POST Multiple Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public async Task<JsonResult> MyAction(IEnumerable<HttpPostedFileBase> files) | |
{ | |
// Do stuff | |
... | |
return new JsonResult() { Data = 'Ok' }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.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