Below is a reference to a simple file upload implementation using the 'connect-busboy' module. I am using an Express setup with Backbone frontend structure.
File uploading with Express 4.0: req.files undefined
I attempted to use this above method for creating a simple file upload and I keep receiving this error:
_stream_readable.js:483
dest.end();
^
TypeError: Cannot call method 'end' of undefined
at IncomingMessage.onend (_stream_readable.js:483:10)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Doing some research I am getting fixes that are irrelevant to my code. I have implemented this solution exactly as defined above with the exception that I am not using a Jade template builder. In addition, I am running the node server off of localhost. Would this cause an issue?
If anyone can help that would be great. Thanks!
P.S. I copy and pasted the code from the original changing only the route and ending redirect in which both are not causing the issue (the server is properly seeing the request come in via the route and it never reaches the redirect since none of the console logs are output.
EDIT - Added code below
server.js route
var express = require('express');
var fs = require('fs');
var busboy = require('connect-busboy');
...
app.use(busboy());
...
app.post('/list', function(req, res) {
console.log("HIT - hasher!");
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/uploads/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('lists');
});
});
});
list.html template
<form enctype="multipart/form-data" action="../list/" method="post" data-abide>
...
<span id="ulbtnview">File Upload</span>
<input id="uplbtn" type="file" class="upload" required>
<small class="error">A valid list file is required.</small>
...
<div class="large-5 columns">
<input id="uploadfile" type="submit" class="button" value="Submit" style="padding-right: 1.5rem; padding-left: 1.5rem; padding-top: 0.5rem; padding-bottom: 0.6rem;"></div>
...
</form>
SOLUTION
The issue is that I needed a 'name' attribute on my input element. Without that the form will process but it will not send the data to the server.
from
<input id="uplbtn" type="file" class="upload" required>
to
<input id="uplbtn" type="file" class="upload" name="file-new" required>
http://www.w3.org/TR/html401/interact/forms.html#successful-controls