question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File event not invoke

See original GitHub issue

I have a simple route handler that accepts file uploads. However, the following code doesn’t work. The route gets called but file event is not fired. I’ve been checking my code against examples and couldn’t find what causing this. I appreciate much anyone could point out what causing this.

restify = require('restify'),
BusBoy  = require('busboy');
request         = require('request');
session = require('restify-session')({
    debug : false,
    ttl : 200           // Time to live in seconds
});
server = restify.createServer();
server.post('/files', uploadFile);    

server.listen(8082);
function uploadFile(req, res, next) {   
    var fileStream = new BusBoy({ headers: req.headers });                      
    req.pipe(fileStream);       
    fileStream.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); 
        res.end();      
    });
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jgujgucommented, Aug 19, 2016

A code snippet from above really helped me out. I’m sending an audiofile from Faraday as a multipart post.

var fileStream = new BusBoy({ headers: req.headers });                      
    req.pipe(fileStream);       
    fileStream.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); 
        res.end();      
    });

And the Rails/Ruby:

url = 'http://localhost:8080/'
conn = Faraday.new(:url => url ) do |c|
    c.request :multipart
    c.request :url_encoded
    c.adapter :net_http
end
payload = { :file => Faraday::UploadIO.new(file_path, content_type) }
response = conn.post '', payload
json = JSON.parse response.body
puts response.body

0reactions
jgujgucommented, Aug 19, 2016

Thank you for your advice, @mscdex. I ended up using the busboy “finish” listener quite successfully here. I changed the var names a bit from before.

var busboy = new Busboy({ headers: req.headers });
req.pipe(busboy);
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    audioFileName = filename + ".ogg";
    var saveTo = path.join("audio_temp", path.basename(audioFileName));
    file.pipe(fs.createWriteStream(saveTo));
});
busboy.on('finish', function() {
    //Finish it
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML input file selection event not firing upon selecting the ...
If I 'open' the same file again in an desktop application, it is usually reloaded, or if some action is done with the...
Read more >
Troubleshoot invocation issues in Lambda
Invocation errors can be caused by issues with request parameters, event structure, function settings, user permissions, resource permissions, or limits. If you ...
Read more >
Introduction to events - Learn web development | MDN
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can...
Read more >
Standard .NET event patterns | Microsoft Learn
Empty that you should use to denote that your event does not contain any additional information ... Invoke(this, new FileFoundArgs(file)); } ...
Read more >
fileevent (n) - Tcl
If the script argument is specified as an empty string then the event handler is deleted, so that no script will be invoked....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found