html:
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异步上传图片文件的方法介绍的详细内容。更多信息请关注PHP中文网其他相关文章!