html:
<form action="url" enctype="multipart/form-data" id="myform" method="post"> <input accept="image/*" id="addfile" type="file" /></form>
jquery:
$("#addfile").on('change', function () { var f = $(this).get(0).files[0]; var form = document.getElementById('myform'); var formData = new FormData(form); formData.append('Filedata', f); var xhr = new XMLHttpRequest(); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.open('POST', form.action); xhr.send(formData);});function uploadComplete(evt){ var data=evt.target.responseText}function uploadFailed(){ alert("上传失败!请重试!"); }
以上所述是小编给大家介绍的JavaScript异步上传图片文件的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!