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("上传失败!请重试!");
}
时间: 2024-10-11 06:03:50