I am facing problem with uploading files using node.js and express framework.
Below is my app.js code:
var express = require('express');
var fs = require('fs');
var busboy = require('connect-busboy');
var app = express();
app.use(busboy());
app.post('/fileupload', function(req, res) {
var fstream;
console.log(req.filename);
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/public/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
});
});
HTML code is
<form id="uploadForm" enctype="multipart/form-data" action="/fileupload" method="post">
<div class="azureD" style="display:none;">
<div class="pull-left">
<label class="labelTemp">Subscription ID</label>
<div class="clickRole addStoTabWid">
<input type="text" id="" placeholder="" style="border:none;width:100%;">
</div>
</div>
<div class="pull-left">
<label class="labelTemp">Upload .pem file</label>
<div class="clickRole addStoTabWid">
<input type="file" name="file" id="file" placeholder="" style="border:none;width:100%;">
</div>
</div>
<div class="modal-footer">
</br>
<input type="submit" value="Upload" name="submit">
</div>
</form>
I am getting the fallowing error in node.js console
TypeError: Cannot read property 'on' of undefined
at IncomingMessage.Readable.pipe (_stream_readable.js:495:7)
at C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\app.js:138:9
at callbacks (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:161:37)
at param (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:170:5)
at Object.router (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:33:10)
at next (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at Object.methodOverride [as handle] (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)
at next (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\proto.js:190:15)
_stream_readable.js:505
dest.end();
^
TypeError: Cannot read property 'end' of undefined
at IncomingMessage.onend (_stream_readable.js:505:9)
at IncomingMessage.g (events.js:199:16)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
I have checked with everything i am not able fix this issue with my code please help me in this issue.
And i want to know any other alternative ways are there upload the file from the browser and store it into mongodb or localdisk
Use the following snippet. It works for you
var upload_path = path.resolve(__dirname + '../../../public/uploads');
var result = {
status: 0,
message: '',
data: ''
};
fs.readFile(req.files.file.path, function (err, data) {
var imageName = Date.now() +"_"+req.files.file.name;
/// If there's an error
if(err){
//error
} else {
var newPath = path.resolve(upload_path, imageName);
fs.writeFile(newPath, data, function (err) {
if(err) {
//error
} else {
fs.unlink(req.files.file.path, function() {
if (err) {
result.status = -1;
result.message = err;
} else {
result.data = imageName;
}
res.jsonp(result);
});
}
});
}
});