错误:解析器错误,已解析 0 个字节(共 4344 个字节)(Node.js)

发布于 2024-12-17 05:30:11 字数 1334 浏览 0 评论 0原文

我正在尝试使用上传程序来上传我的文件。我使用的代码是

app.post('/photos',loadUser, function(req, res) {
  var post = new Post();
  req.form.complete(function(err, fields, files) {
    if(err) {
      console.log(err);
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
      post.filename=files.file.filename;
      post.file=files.file.path;
      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else {
          post.save(function(err,docs) { 
        req.flash('info', 'information  Saved');
            res.redirect('/photos');
          });
        }
      });
    }
  });
});

当我删除 loadUser 方法时,一切工作正常,但是当我使用 loadUser 方法时,它给了我一个错误。错误的控制台信息是:

Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)

仅当我使用 loadUser 函数时才会导致该错误,如果我删除 loadUser 函数,则一切正常。 我不知道这背后的原因并且被困住了。有人可以帮我吗?

I am trying to use an upload program to upload my files. The code that I use is

app.post('/photos',loadUser, function(req, res) {
  var post = new Post();
  req.form.complete(function(err, fields, files) {
    if(err) {
      console.log(err);
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
      post.filename=files.file.filename;
      post.file=files.file.path;
      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else {
          post.save(function(err,docs) { 
        req.flash('info', 'information  Saved');
            res.redirect('/photos');
          });
        }
      });
    }
  });
});

When I remove loadUser method everything is working fine, but when I use the loadUser method it is giving me an error. The console information of the error is:

Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)

The error is caused only when i use loadUser function, if i remove the loadUser Funciton everything is working fine.
I don't know the reason behind this and am stuck. Can anyone help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九命猫 2024-12-24 05:30:12

您试图在一切之前执行数据库操作,这给您带来了问题。尝试以下代码:

app.post('/potos', function(req, res, next) {
  //req.form.pause();
  req.form.complete(function(err, fields, files) {
    if(err) {
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
      var post = new Post();
      post.filename=files.file.filename;
      post.file=files.file.path;
      post.created_at = new Date();
      post.user_id = req.session.user_id;

      function postCreationFailed() {
        req.flash('error', 'Unable to Download ');
        res.render('photos/new', {
          locals: {
             post: new Post(),currentUser: req.session.user_id
          }
        });
      }

      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else { 
                 console.log('\nuploaded %s to %s',  files.file.filename, files.file.path);
                 post.save(function(err) {
                   if (err)
                return postCreationFailed();
                   req.flash('info', 'photos Succesfully Uploaded');
               res.redirect('/user/photos/'+post.user_id);
                });
              }
      });
    }
  });
  req.form.on('progress', function(bytesReceived, bytesExpected){
    var percent = (bytesReceived / bytesExpected * 100) | 0;
    process.stdout.write('Uploading: %' + percent + '\r');
  });

});

祝你好运...

You are trying to perform database operation before everything, which is creating problems for you. Try the following code:

app.post('/potos', function(req, res, next) {
  //req.form.pause();
  req.form.complete(function(err, fields, files) {
    if(err) {
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
      var post = new Post();
      post.filename=files.file.filename;
      post.file=files.file.path;
      post.created_at = new Date();
      post.user_id = req.session.user_id;

      function postCreationFailed() {
        req.flash('error', 'Unable to Download ');
        res.render('photos/new', {
          locals: {
             post: new Post(),currentUser: req.session.user_id
          }
        });
      }

      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else { 
                 console.log('\nuploaded %s to %s',  files.file.filename, files.file.path);
                 post.save(function(err) {
                   if (err)
                return postCreationFailed();
                   req.flash('info', 'photos Succesfully Uploaded');
               res.redirect('/user/photos/'+post.user_id);
                });
              }
      });
    }
  });
  req.form.on('progress', function(bytesReceived, bytesExpected){
    var percent = (bytesReceived / bytesExpected * 100) | 0;
    process.stdout.write('Uploading: %' + percent + '\r');
  });

});

Good luck...

情仇皆在手 2024-12-24 05:30:11

该问题的另一个可能原因是这一行:

request.setEncoding( "utf8" );

Another possible cause for the problem is in this line:

request.setEncoding( "utf8" );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文