Node.js上传到Azure Blob存储问题 [英] Node.js upload to Azure Blob Storage issues
问题描述
我是Node.js/Express的新手,正在尝试将简单文件上传到Azure blob存储.我正在使用AngularJS作为前端表单.尝试上传文件时,出现以下错误:
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:无法读取未定义的属性"myfile"
TypeError: Cannot read property 'myfile' of undefined
我看到了一些示例,这些示例利用了名为multiparty的节点模块,但不确定它是否适用于此基本上传需求.来自jQuery/前端JavaScript世界,在某些情况下,我需要对文件进行base64编码以便上传.在这种情况下是这种情况吗?还是我只是缺少一个简单的传递,以便Node/Express可以读取"myfile"?
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"?
由于Express拥有自己的路由器,而我已经安装了AngularJS路由,这可能会导致问题吗?
Since Express has its own router and I have AngularJS routing in place, could that be causing the issue?
这是我在AngularJS视图中的表单:
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>
这是我的server.js配置.请注意,我正在使用Azure blob存储:
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);
}
});
});
更新1:
如果使用以下命令,它将生成一个POST 200 OK,但是当我使用Azure Storage Explorer之类的东西查看blob容器时,该blob就不存在了.
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');
});
推荐答案
在GitHub/Azure/azure-sdk-for-node中有一个示例"blobuploader"
There is a sample "blobuploader" in GitHub/Azure/azure-sdk-for-node https://github.com/Azure/azure-sdk-for-node/tree/master/examples/blobuploader.
我参考了此示例和您的代码,以在下面做一个简单的例子:
I refered to this sample and your code to make a simple example below:
var azure = require('azure');
var storageAccount = '<Your Storage Account>';
var accessKey = '<Your Access Key>'
var blobSvc = azure.createBlobService(storageAccount, accessKey);
var express = require('express');
var formidable = require('formidable'); // the same node module as Azure sample
var app = express();
app.use(express.static('public')); // contains a form page using the same code as yours
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.post('/upload', function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
var options = {
contentType: files.myfile.type,
metadata: { fileName: files.myfile.name }
};
blobSvc.createBlockBlobFromLocalFile('mycontainer', files.myfile.name, files.myfile.path, options, function(error, result, response) {
if(!error){
// file uploaded
res.send('file uploaded!');
}
});
});
//console.log(req);
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
有效!
最好的问候.
这篇关于Node.js上传到Azure Blob存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!