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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:07:26+00:00 2026-06-08T07:07:26+00:00

I’m trying to build a quick and simple image uploading service with Node, that

  • 0

I’m trying to build a quick and simple image uploading service with Node, that takes the received images and saves them to Mongo’s GridFS.

GridFS get requires a Buffer object NodeJS Mongo Driver GridFS put

The question is pretty simple: how do I exactly cast/transform the received request body into a proper buffer.

My code so far (only the important pieces):
api.js

var express = require('express');
var connect = require('connect');

var app = module.exports = express.createServer();

app.configure(function(){
  app.use(express.bodyParser()); 
  app.use(express.methodOverride());
  app.use(app.router);
});

var upload = require('./upload.js');
app.post('/upload', upload.upload);

upload.js

exports.upload = (function(req, res, next){
  console.log("Uploading image...");
  // Create buffer
  // Rest of the code
}

I’ve tried:

  var buffer = new Buffer(util.inspect(req.body),'binary');

Creates the buffer, but it has a wrong size and probably not the correct content since util.inspect is obviously not the right way to go.

And:
var buffer = new Buffer(req.body);
Result:

 [Decode error - output not utf-8][Decode error - output not utf-8]

Buffer length = 0

I’m quite new to both Node and JavaScript developing in general, so probably I’m missing something quite simple, don’t hesitate to point the obvious 🙂

Thanks!

  • 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-08T07:07:28+00:00Added an answer on June 8, 2026 at 7:07 am

    First, remember that Express is built on top of Connect, which is the library that handles a large amount of the lower-level HTTP work, and it’s where bodyParser() comes from.

    The body parser middleware internally uses Formidable to parse file uploads.

    Formidable’s default behavior is to write uploaded files directly to disk – in other words, you don’t actually have access to the uploaded file stream within your route handler. You get the values of any regular form fields (<input>s) sent along in req.body, and you get uploaded file paths via req.files, but you don’t get file content.

    The easy answer here is to simply read the file from disk and use that to save into Mongo, remembering to delete the temporary file when done. Of course, this introduces the unnecessary intermediate step of writing the file upload to a temporary folder and then loading to Mongo.

    If you want to stream file data directly into Mongo, you have more of a challenge in front of you. You’ll have to write your own middleware to parse the upload stream.

    This is actually relatively easy. You can just start with the internal Connect body parser implementation—using Formidable to do the heavy lifting—and use the onPart API to pass the stream back to your route handler so that you can pass it off to the Mongo driver.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.