node.js - 使用发送事件以编程方式创建的 Dropzone 发送附加数据

标签 node.js angularjs dropzone.js

我有以下(例如简化的)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/

相关文章:

javascript - 使用 Promises 响应来自 Node.js 的请求

javascript - 我们应该在 Angular js 版本 1.3.14 中使用 $locationProvider 吗

dropzone.js - "myDropzone.emit is not a function"Dropzone js

javascript - 在nodejs中与集群共享流

node.js - 可写流完成了吗?

node.js - 这个 npm 开关如何修复 Windows 路径长度问题

javascript - 为什么 Angularjs 指令会变成 html 注释?

javascript - Angular 在自定义指令的后链接中无法识别 Jquery 的 css 函数

javascript - 使用 Fluentlenium 在 dropzone.js 中上传文件

asp.net-mvc - 与其他表单数据结合使用时,文件未使用 dropzone.js 发布