写一段代码实现网页弹窗选择文件上传,并将文件保存至服务器指定位置
时间: 2023-03-13 07:29:07 浏览: 219
<script>
function uploadFile(){
var fileUpload = document.getElementById("fileUpload");
fileUpload.addEventListener("change", function () {
var file = fileUpload.files[0];
if (file) {
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php');
xhr.send(formData);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert('上传成功!');
} else {
alert('上传失败!');
}
}
}
}
}, false);
}
</script>
阅读全文