我有以下(例如简化的)angular 指令,它创建了一个 dropzone
directives.directive('dropzone', ['dropZoneFactory', function(dropZoneFactory){
'use strict';
return {
restrict: 'C',
link : function(scope, element, attrs){
new Dropzone('#'+attrs.id, {url: attrs.url});
var myDropZone = Dropzone.forElement('#'+attrs.id);
myDropZone.on('sending', function(file, xhr, formData){
//this gets triggered
console.log('sending');
formData.userName='bob';
});
}
}
}]);
如您所见,sending
事件处理程序我正在尝试发送用户名(“bob”)以及上传的文件。但是,我似乎无法在我的路由中间件中检索它,因为 req.params
作为空数组返回(我也尝试过 req.body
)。
我的 Node 路由
{
path: '/uploads',
httpMethod: 'POST',
middleware: [express.bodyParser({ keepExtensions: true, uploadDir: 'uploads'}),function(request,response){
// comes back as []
console.log(request.params);
//this sees the files fine
console.log(request.files);
response.end("upload complete");
}]
}
这是文档对 sending
事件的描述
Called just before each file is sent. Gets the xhr object and the formData objects as second and third parameters, so you can modify them (for example to add a CSRF token) or add additional data.
编辑
我暂时放弃了程序化方法。我有两种形式提交到同一个端点,一种是普通的只有 post 的形式,另一种是 dropzone 的形式。两者都有效,所以我认为这不是端点的问题,而是我处理“发送”事件的方式的问题。
//Receives the POST var just fine
form(action="http://127.0.0.1:3000/uploads", method="post", id="mydz")
input(type="hidden", name="additionaldata", value="1")
input(type="submit")
//With this one I can get the POST var
form(action="http://127.0.0.1:3000/uploads", method="post", id="mydz2", class="dropzone")
input(type="hidden", name="additionaldata", value="1")
最佳答案
好吧,我真的想通了,感谢Using Dropzone.js to upload after new user creation, send headers
发送
事件:
myDropZone.on('sending', function(file, xhr, formData){
formData.append('userName', 'bob');
});
与 formData.userName = 'bob'
相反,后者由于某种原因不起作用。
关于node.js - 使用发送事件以编程方式创建的 Dropzone 发送附加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21942314/