GithubHelp home page GithubHelp logo

Create Directory before save file about busboy HOT 4 CLOSED

mscdex avatar mscdex commented on August 20, 2024
Create Directory before save file

from busboy.

Comments (4)

mscdex avatar mscdex commented on August 20, 2024 1

I see the problem: you've added a 'data' handler right after you called the async mkdirp. This causes data to be released immediately. Remove that and you should be fine.

from busboy.

mscdex avatar mscdex commented on August 20, 2024

What node version are you using?

from busboy.

metd avatar metd commented on August 20, 2024

node v0.10.16
expressjs 3.4.4
connect-busboy 0.0.1

may be problem is about synchronous or asynchronous,
when i use mkdirp in synchronous mode, it's work fine.
problem happened when i switch to asynchronous mode

This is my code.

var express = require('express');
var http = require('http');
var busboy = require('connect-busboy');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var inspect = require('util').inspect;

var app = express();

app.use(
    busboy(
    {
        limits : {
            fileSize : 10 * 1024 * 1024
        }
    }
));

app.get('/upload', function(req, res, next){
    res.send('<html><head></head><body>\
            <form method="POST" enctype="multipart/form-data">\
             <input type="text" name="textfield"><br />\
             <input type="file" name="filefield"><br />\
             <input type="submit">\
           </form>\
         </body></html>');
});

app.post('/upload', function(req, res, next){
    req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname +']: filename: ' + filename + ', encoding: ' + encoding);

        var rootDir = path.dirname(require.main.filename);
        var uploadDir = 'media/photos';
        var saveTo = path.join(rootDir, uploadDir);

        mkdirp(saveTo, function(err) {
            if (err) {
                console.error(err);
                return next(err);
            } else {
                console.log('Save File..');
                saveTo = path.join(saveTo, path.basename(filename));
                file.pipe(fs.createWriteStream(saveTo));
            }
        });

        file.on('data', function(data) {
            console.log('File [' + fieldname + '] got ' + data.length
                    + ' bytes');
        });
        file.on('end', function() {
            console.log('File [' + fieldname + '] Finished');
        });

    });

    req.busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    });

    req.busboy.on('end', function() {
        console.log('Done parsing form!');
        res.send(200, 'OK');
    });

    req.pipe(req.busboy);
});


http.createServer(app).listen(4000);

from busboy.

metd avatar metd commented on August 20, 2024

Brian White
Thank you so much.

from busboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.