<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="file" class="myfile" />
进度:<progress value="0" max="100"></progress> <span class="percent">0%</span>
速度:<span class="speed">20b/s</span>
<button>点击上传</button>
<button>取消上传</button>
</body>
<script>
let xhr = new XMLHttpRequest();
let btns = document.querySelectorAll("button");
let stime;
let sloaded;
btns[0].onclick = function () {
let file = document.querySelector(".myfile").files[0];
let form = new FormData();
form.append("myfile", file);
xhr.open("post", "/fileUpload", true);
xhr.onload = function () {
console.log(xhr.responseText);
}
xhr.upload.onloadstart = function(){
console.log("开始上传");
stime = new Date().getTime();
sloaded = 0;
}
xhr.upload.onprogress = function(evt){
let endTime = new Date().getTime();
// 时间差;
let dTime = (endTime - stime)/1000;
// 当前时间内上传的文件大小
let dloaded = evt.loaded - sloaded;
let speed = dloaded/dTime;
let unit = "b/s";
stime = new Date().getTime();
sloaded = evt.loaded;
if(speed/1024>1){
unit = "kb/s";
speed = speed/1024;
}
if(speed/1024>1){
unit = "mb/s";
speed = speed/1024;
}
document.querySelector(".speed").innerHTML = speed.toFixed(2)+unit;
// console.log(speed.toFixed(2));
// console.log("正在上传");
// 当前文件上传的大小evt.loaded
// 需要上传文件的大小
let percent = (evt.loaded/evt.total*100).toFixed(0);
// console.log(percent);
document.querySelector("progress").value = percent;
document.querySelector(".percent").innerHTML = percent+"%";
}
xhr.upload.onload = function(){
console.log("上传成功");
}
xhr.upload.onloadend = function(){
console.log("上传完成");
}
xhr.upload.onabort = function(){
console.log("取消上传");
}
xhr.send(form);
}
btns[1].onclick = function(){
xhr.abort();
}
</script>
</html>
const Koa = require("koa");
const static = require("koa-static");
const Router = require("koa-router");
const koaBody = require("koa-body");
const fs = require("fs");
const usersData = require("./data/users.json");
let app = new Koa();
app.use(static(__dirname+"/static"));
app.use(koaBody({
multipart:true
}));
let router = new Router();
router.get("/",(ctx,next)=>{
ctx.body = "hello";
})
router.get("/checkUserName",(ctx,next)=>{
console.log(ctx.query.username);
let res = usersData.find(v=>v.username==ctx.query.username);
if(res){
ctx.body = {
status:1,
info:"用户名正确"
};
}else{
ctx.body = {
status:2,
info:"用户名错误"
};
}
})
router.get("/get/:id",(ctx,next)=>{
console.log(ctx.params);
ctx.body = {
status:1,
info:"请求成功"
}
})
router.post("/post",(ctx,next)=>{
console.log(ctx.request.body);
ctx.body = {
status:1,
info:"post请求成功"
}
})
router.get("/xml",(ctx,next)=>{
// ctx.set("content-type","text/xml");
ctx.body = `<?xml version='1.0' encoding='utf-8' ?>
<books>
<nodejs>
<name>nodejs实战</name>
<price>56元</price>
</nodejs>
<react>
<name>react入门</name>
<price>50元</price>
</react>
</books>
`
})
router.post("/upload",(ctx,next)=>{
console.log(ctx.request.body);
console.log(ctx.request.files.img);
let fileData = fs.readFileSync(ctx.request.files.img.path);
fs.writeFileSync("static/imgs/"+ctx.request.files.img.name,fileData);
ctx.body = "请求成功";
})
router.post("/fileUpload",(ctx,next)=>{
console.log(ctx.request.files);
let fileData = fs.readFileSync(ctx.request.files.myfile.path);
fs.writeFileSync("static/imgs/"+ctx.request.files.myfile.name,fileData);
ctx.body = "请求成功";
})
app.use(router.routes());
app.listen(3000);
网友评论