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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:39:24+00:00 2026-05-23T02:39:24+00:00

I created an upload script in node.js using express/formidable. It basically works, but I

  • 0

I created an upload script in node.js using express/formidable. It basically works, but I am wondering where and when to check the uploaded file e. g. for the maximum file size or if the file´s mimetype is actually allowed.

My program looks like this:

app.post('/', function(req, res, next) {
    req.form.on('progress', function(bytesReceived, bytesExpected) {
        // ... do stuff
    });

    req.form.complete(function(err, fields, files) {
        console.log('\nuploaded %s to %s',  files.image.filename, files.image.path);
        // ... do stuff    
    });
});

It seems to me that the only viable place for checking the mimetype/file size is the complete event where I can reliably use the filesystem functions to get the size of the uploaded file in /tmp/ – but that seems like a not so good idea because:

  • the possibly malicious/too large file is already uploaded on my server
  • the user experience is poor – you watch the upload progress just to be told that it didnt work afterwards

Whats the best practice for implementing this? I found quite a few examples for file uploads in node.js but none seemed to do the security checks I would need.

  • 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-23T02:39:25+00:00Added an answer on May 23, 2026 at 2:39 am

    With help from some guys at the node IRC and the node mailing list, here is what I do:

    I am using formidable to handle the file upload. Using the progress event I can check the maximum filesize like this:

    form.on('progress', function(bytesReceived, bytesExpected) {
        if (bytesReceived > MAX_UPLOAD_SIZE) {
            console.log('### ERROR: FILE TOO LARGE');
        }
    });
    

    Reliably checking the mimetype is much more difficult. The basic Idea is to use the progress event, then if enough of the file is uploaded use a file --mime-type call and check the output of that external command. Simplified it looks like this:

    // contains the path of the uploaded file, 
    // is grabbed in the fileBegin event below
    var tmpPath; 
    
    form.on('progress', function validateMimetype(bytesReceived, bytesExpected) {
        var percent = (bytesReceived / bytesExpected * 100) | 0;
    
        // pretty basic check if enough bytes of the file are written to disk, 
        // might be too naive if the file is small!
        if (tmpPath && percent > 25) {
            var child = exec('file --mime-type ' + tmpPath, function (err, stdout, stderr) {
                var mimetype = stdout.substring(stdout.lastIndexOf(':') + 2, stdout.lastIndexOf('\n'));
    
                console.log('### file CALL OUTPUT', err, stdout, stderr);
    
                if (err || stderr) {
                    console.log('### ERROR: MIMETYPE COULD NOT BE DETECTED');
                } else if (!ALLOWED_MIME_TYPES[mimetype]) {
                    console.log('### ERROR: INVALID MIMETYPE', mimetype);
                } else {
                    console.log('### MIMETYPE VALIDATION COMPLETE');
                }
            });
    
            form.removeListener('progress', validateMimetype);
        }
    });
    
    form.on('fileBegin', function grabTmpPath(_, fileInfo) {
        if (fileInfo.path) {
            tmpPath = fileInfo.path;
            form.removeListener('fileBegin', grabTmpPath);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file upload script that works [finally] but want to have users
hii i am using ajax file upload in this code <script type=text/javascript src=fileuploader.js></script> <script
We have created a web application, using ASP.NET, that allows users to upload documents
I have a flash upload script, that uses a .php file as the processor.
I asked a similar question over here:( Upload clocking script ), but then changed
Does anyone know of any methods to create a file upload progress bar in
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I created a few mediawiki custom tags, using the guide found here http://www.mediawiki.org/wiki/Manual:Tag_extensions I
I have a ajax image upload script which i found here http://www.fengcool.com/2009/06/ajax-form-upload-local-image-file-without-refresh/ The problem
In my upload photo script, at the processing part, when it moves the upload

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.