将额外信息传递给 multer 上传 API
passing extra info to multer Upload API
我在node.js中使用multer方式上传,这里有详细解释https://ciphertrick.com/2015/12/07/file-upload-with-angularjs-and-nodejs/
我正在尝试传递另一个包含请求数据的信息,该信息称为 invoiceId:
Upload.upload({
url: 'http://localhost:4000/api/ubiq/listInvoiceAttachedFiles/attach', //webAPI exposed to upload the file
data: {file: file, invoiceId:invoiceId} //pass file as data, should be user ng-model
}).then(function (resp) { //upload function returns a promise
if (resp.data.error_code === 0) { //validate success
$window.alert('Success ' + resp.config.data.file.name + ' uploaded');
console.log(resp.config.data.file); ....etc
但是我在服务器端 req.body 变空了:
/** API path that will upload the files */
server.post('/api/ubiq/listInvoiceAttachedFiles/attach', function(req, res) {
console.log(req.body);
security.verifyPermission("/api/ubiq/listInvoiceAttachedFiles/attach", req.session.currentUser, true /*isInSession*/).then(function (successInfo) {
if (!successInfo.isAllowed) {
console.log('not allowed');
return res.json(apiHelp.notAllowed());
}
我做错了什么?
以下代码段是我的工作代码,我已经在我的应用程序中多次测试和使用。
Multer 有两个存储引擎:
- 磁盘存储
- 内存存储
我使用了 DiskStorage,它可以更好地控制文件的磁盘存储。
var express = require("express");
var app = express()
var router = express.Router();
var multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, dirPath);
},
filename: function (req, file, cb) {
var datetimestamp = Date.now() + Math.floor(Math.random() * (1 - 99999999999 + 1)) + 9999999999999;
cb(null, datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1].toLowerCase());
}
});
var infoUpload = multer({storage: storage});
router
.route(API_PATH)
.post(infoUpload.array("file"), function (req, res) {
console.log(req.body);
});
以下是几个链接,可以为您提供简要说明。
http://derpturkey.com/node-multipart-form-data-explained/
http://alexkatz.me/posts/image-upload-with-node-and-multer/
希望对解决问题有所帮助。
在上传函数的回调中添加 re-attaches 正文对象。
您的数据应该在此行之后可用。
https://github.com/rahil471/file-upload-with-angularjs-and-nodejs/blob/master/server/app.js#L35
我在node.js中使用multer方式上传,这里有详细解释https://ciphertrick.com/2015/12/07/file-upload-with-angularjs-and-nodejs/
我正在尝试传递另一个包含请求数据的信息,该信息称为 invoiceId:
Upload.upload({
url: 'http://localhost:4000/api/ubiq/listInvoiceAttachedFiles/attach', //webAPI exposed to upload the file
data: {file: file, invoiceId:invoiceId} //pass file as data, should be user ng-model
}).then(function (resp) { //upload function returns a promise
if (resp.data.error_code === 0) { //validate success
$window.alert('Success ' + resp.config.data.file.name + ' uploaded');
console.log(resp.config.data.file); ....etc
但是我在服务器端 req.body 变空了:
/** API path that will upload the files */
server.post('/api/ubiq/listInvoiceAttachedFiles/attach', function(req, res) {
console.log(req.body);
security.verifyPermission("/api/ubiq/listInvoiceAttachedFiles/attach", req.session.currentUser, true /*isInSession*/).then(function (successInfo) {
if (!successInfo.isAllowed) {
console.log('not allowed');
return res.json(apiHelp.notAllowed());
}
我做错了什么?
以下代码段是我的工作代码,我已经在我的应用程序中多次测试和使用。
Multer 有两个存储引擎:
- 磁盘存储
- 内存存储
我使用了 DiskStorage,它可以更好地控制文件的磁盘存储。
var express = require("express");
var app = express()
var router = express.Router();
var multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, dirPath);
},
filename: function (req, file, cb) {
var datetimestamp = Date.now() + Math.floor(Math.random() * (1 - 99999999999 + 1)) + 9999999999999;
cb(null, datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1].toLowerCase());
}
});
var infoUpload = multer({storage: storage});
router
.route(API_PATH)
.post(infoUpload.array("file"), function (req, res) {
console.log(req.body);
});
以下是几个链接,可以为您提供简要说明。
http://derpturkey.com/node-multipart-form-data-explained/
http://alexkatz.me/posts/image-upload-with-node-and-multer/
希望对解决问题有所帮助。
在上传函数的回调中添加 re-attaches 正文对象。
您的数据应该在此行之后可用。 https://github.com/rahil471/file-upload-with-angularjs-and-nodejs/blob/master/server/app.js#L35