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 8318497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:59:43+00:00 2026-06-08T21:59:43+00:00

Using nodeJs with express. The POST data is being parsed by body parser. Now

  • 0

Using nodeJs with express.

The POST data is being parsed by body parser. Now i want to save the received image file in a folder of express application named images. The image is being posted to nodeJs app. When i look at the res.body variable in the callback function it contains the image, but i am confused how to save it.

I have looked on stackflow but could not find any answer. So posted myself.

I have tried using the fs library and its fs.writeFile() function. It saves the file but it is all corrupted.

image1 is the name of image in posted data. Using bodyparser()

var image = req.body.image1;
        fs.writeFile('images/newImage.jpg', image, function(err){
            if (err) throw err;
            console.log('It is saved');
        });

This saves a file in images directory but it is not the image.

I am using curl to post data to the nodejs application, the code is

$curl = curl_init();
$data = array('name' => 'First','file' => "image1=@".__dir__."/images/1.jpg");

curl_setopt($curl, CURLOPT_URL, "http://localhost:3000/api/");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_exec($curl);
curl_close($curl);

I am logging both of the two console.log functions

   console.log(req.files);
   console.log(req.body);

and the log give

{}
{ name: 'First',
  file: 'image1=@C:\\xampp\\htdocs\\curltest/images/1.jpg' }

Please help this is driving me crazy :-/

  • 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-06-08T21:59:44+00:00Added an answer on June 8, 2026 at 9:59 pm

    First of all the correct parameter order of fs.writeFile is the filename first then the content.

    If you’re using express.bodyParser() you’ll have the uploaded files in the req.files field.

    And of course you should write the callback:

    fs.writeFile('newImage', req.files.image, function (err) {
      if (err) throw err;
      console.log("It's saved");
    });
    

    Update

    You should also check your HTML side, because the encoding of your form must be multipart/form-data.

    Example:

    <form action="/somewhere/to/post" method="post" enctype="multipart/form-data">
      <input type="file" name="image1"/>
      <input type="submit"/>
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of nodejs applications running on Express using the code below.
I am using Node.js (with Express.js) to pass a JSON data object from the
I'm using Node.js with Express, Connect, and Jade. I want to provide an absolute
I'm using Node.js and Express, and I want to pass a local variable in
I am using Nodejs and Express Js. Also I add NowJS to the Express
being a beginner i m trying to develop a very simple service using express
I want to build a chat system in nodeJs + MYSQL using php. It
I am using express (web framework for node.js) with ejs. Now I'd like to
I want to rewrite a complete community website in nodejs,express and nowjs with mongodb.
I am using express in nodejs, and am trying to keep my view rendering

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.