Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7398149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:50:08+00:00 2026-05-29T03:50:08+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:50:09+00:00Added an answer on May 29, 2026 at 3:50 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to use jquery file upload to post files to a fileserver written
I'm trying to use S7FTPRequest to upload files to an FTP server: S7FTPRequest *ftp
I am trying to use the multiple upload attribute of HTML5 to upload files.
I'm trying to use attachment_fu to upload files to a directory outside of the
I am trying to use plupload to upload files in ASP.NET. My upload handler
I am trying to use the paperclip plugin in my rails app to upload
I'm trying to use blueimp's jQuery file upload script . The files are sent
I'm trying to use the paperclip gem to upload photos in my rails project.
I am trying to use WCF service to upload GZip files. Im trying to
I'm trying to compile a simple Python program, that uploads files to an S3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.