用来解析上传文件的
useXMLHttpRequest = (url, formData) => {
let percent = "Not start";
let xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", (rs) => {
if(rs.lengthComputable) {
percent = (rs.loaded / rs.total * 100).toFixed(2);
this.setState({status: percent});
}
}, false);
xhr.addEventListener("readystatechange", () => {
let rs = xhr;
if(rs.status !== 200) {
console.log("failed")
} else if(rs.status === 4) {
console.log("success")
}
});
xhr.open("post", url, true);
xhr.send(formData);
}
router.post('/upload', function(req, res, next) {
let count = 0;
console.log("upload")
res.header("Access-Control-Allow-Origin", "*");
let options = {
autoFiles: true,
uploadDir: "tmp"
}
let form = new multiparty.Form(options);
form.on("close", () => {
console.log("upload complete");
res.json({info: "success"});
})
form.on("error", (err) => {
res.json({info: err});
})
form.parse(req);
});
网友评论