我是js和html编程的新手。我正在使用以下代码创建一个显示文件上传进度的进度条。但它不适用于tryit编辑器
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<style>
.progress-wrapper {
width:100%;
}
.progress-wrapper .progress {
background-color:green;
width:0%;
padding:5px 0px 5px 0px;
}
</style>
<script>
function postFile() {
var formdata = new FormData();
formdata.append('file1', $('#file1')[0].files[0]);
var request = new XMLHttpRequest();
request.upload.addEventListener('progress', function (e) {
var file1Size = $('#file1')[0].files[0].size;
if (e.loaded <= file1Size) {
var percent = Math.round(e.loaded / file1Size * 100);
$('#progress-bar-file1').width(percent + '%').html(percent + '%');
}
if(e.loaded == e.total){
$('#progress-bar-file1').width(100 + '%').html(100 + '%');
}
});
request.open('post', '/echo/html/');
request.timeout = 45000;
request.send(formdata);
}
</script>
</head>
<form id="form1">
<input id="file1" type="file" />
<div class="progress-wrapper">
<div id="progress-bar-file1" class="progress"></div>
</div>
<button type="button" onclick="postFile()">Upload File</button>
</form>
</html>
我也试过我的本地系统,它也没有在这里工作。我做错了什么?请告诉我如何改进代码。我想显示一个进度条,显示使用ajax调用的文件上传百分比。
答案 0 :(得分:1)
在撰写任何head
之前,您需要在JavaScript
中添加JQuery
的引用。请参阅下面的正确版本。
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<style>
.progress-wrapper {
width:100%;
}
.progress-wrapper .progress {
background-color:green;
width:0%;
padding:5px 0px 5px 0px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script>
function postFile() {
var formdata = new FormData();
formdata.append('file1', $('#file1')[0].files[0]);
var request = new XMLHttpRequest();
request.upload.addEventListener('progress', function (e) {
var file1Size = $('#file1')[0].files[0].size;
if (e.loaded <= file1Size) {
var percent = Math.round(e.loaded / file1Size * 100);
$('#progress-bar-file1').width(percent + '%').html(percent + '%');
}
if(e.loaded == e.total){
$('#progress-bar-file1').width(100 + '%').html(100 + '%');
}
});
request.open('post', '/echo/html/');
request.timeout = 45000;
request.send(formdata);
}
</script>
</head>
<form id="form1">
<input id="file1" type="file" />
<div class="progress-wrapper">
<div id="progress-bar-file1" class="progress"></div>
</div>
<button type="button" onclick="postFile()">Upload File</button>
</form>
</html>