美文网首页
multiparty

multiparty

作者: SpinachC | 来源:发表于2018-12-16 13:01 被阅读3次

    用来解析上传文件的

      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);
    
    });
    

    相关文章

      网友评论

          本文标题:multiparty

          本文链接:https://www.haomeiwen.com/subject/wgotkqtx.html