<script><br />
document.querySelector('#afile').addEventListener('change', function(e) {<br />
var file = this.files[0];
<p> var fd = new FormData();<br />
fd.append("afile", file);<br />
// These extra params aren't necessary but show that you can include other data.<br />
fd.append("username", "Groucho");<br />
fd.append("accountnum", 123456);
<p> var xhr = new XMLHttpRequest();<br />
xhr.open('POST', 'handle_file_upload.php', true);<br />
<br />
xhr.upload.onprogress = function(e) {<br />
if (e.lengthComputable) {<br />
var percentComplete = (e.loaded / e.total) * 100;<br />
console.log(percentComplete + '% uploaded');<br />
}<br />
};
<p> xhr.onload = function() {<br />
if (this.status == 200) {<br />
var resp = JSON.parse(this.response);
<p> console.log('Server got:', resp);
<p> var image = document.createElement('img');<br />
image.src = resp.dataUrl;<br />
document.body.appendChild(image);<br />
};<br />
};
<p> xhr.send(fd);<br />
}, false);<br />
</script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn