Skip to content

Instantly share code, notes, and snippets.

@Nicksxs
Last active September 2, 2015 01:20
Show Gist options
  • Save Nicksxs/06ae275fd823f53ceeeb to your computer and use it in GitHub Desktop.
Save Nicksxs/06ae275fd823f53ceeeb to your computer and use it in GitHub Desktop.
ajax simulate form contain file to submit
<script type="text/javascript">
$(document).ready(function () {
$('#form1').submit(function () {
var formdata = new FormData();
var fileObj = document.getElementById("fileToUpload2").files;
for (var i = 0; i < fileObj.length; i++)
formdata.append("file" + i, fileObj[i]);
$.ajax({
type: 'POST',
url: '/Home/Upload2',
data: formdata,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false
}).then(function () {
alert('done');
}, function () {
//failCal
});
return false;
});
});
function ajaxUpload() {
$("#form1").submit();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment