Last active
September 2, 2015 01:20
-
-
Save Nicksxs/06ae275fd823f53ceeeb to your computer and use it in GitHub Desktop.
ajax simulate form contain file to submit
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
<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