I am newer to Node.js/Express and am attempting a simple file upload to Azure blob storage. I am using AngularJS for the front end form. When I attempt to upload a file I receive the following error:
TypeError: Cannot read property 'myfile' of undefined
I have seen some examples where they are leveraging a node module named multiparty but am unsure if it applies to this basic upload need. Coming from the jQuery/front end JavaScript world, I would need to base64 encode my file for upload in some cases. Is that the case in this instance? Or am I just missing a simple pass through so that Node/Express can read "myfile"?
Since Express has its own router and I have AngularJS routing in place, could that be causing the issue?
Here is my form in my AngularJS view:
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
<input type="submit" value="Upload" />
</form>
Here is my server.js config. Please note that I am using Azure blob storage:
var express = require('express');
var azure = require('azure-storage');
// Initialize Express App
var app = express();
var port = process.env.port || 1337;
app.use(express.static(__dirname + "/public")).listen(port);
var accessKey = 'mykey';
var storageAccount = 'myblobaccount';
var blobSvc = azure.createBlobService(storageAccount,accessKey);
// Upload to Azure Blob Storate
app.post('/upload', function (req, res) {
var path = req.files.myfile.path;
blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob.png', path, function (error, result, response) {
if (!error) {
console.log("Uploaded" + result);
// file uploaded
}
else {
console.log(error);
}
});
});
Update 1:
If I use the following, it generates a POST 200 OK, but when I look at my blob container using something like Azure Storage Explorer, the blob is not there.
app.post('/upload2', function (req, res) {
var multiparty = require('multiparty');
var container = 'mycontainer';
var form = new multiparty.Form();
form.on('part', function (part) {
if (part.filename) {
var size = part.byteCount - part.byteOffset;
var name = part.filename;
blobSvc.createBlockBlobFromStream(container, name, part, size, function (error) {
if (error) {
//res.send(' Blob create: error ');
}
});
} else {
form.handlePart(part);
}
});
form.parse(req);
res.send('OK');
});