Skip to content

Trying to fix passing formData from form to middleware #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2014

Conversation

gsarwohadi
Copy link
Contributor

I needed to use 'formData' sent from the client and since the middleware isn't collecting it properly, I tried to make a fix.

This what I did:
Collect formData on 'field' formidable event in UploadHandler.prototype.post method to a req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event. So on upload.on("end", function (fileInfo)), I can have the extra property from formData in fileInfo.

Example use:

In the client:

<form id="formupload" action="/temp" method="post" enctype="multipart/form-data">
    <input type="hidden" name="user_id" value="some user id">
    <input id="fileupload" type="file" name="files[]" data-url="/temp" multiple>
</form>

In the server:

upload.on("end", function (fileInfo)
    {
        var filemanager = upload.fileManager({
            targetDir: __dirname + "/public/userfiles/" + fileInfo.user_id,
            targetUrl: "/userfiles/" + fileInfo.user_id
        });
        filemanager.move(fileInfo.name, "", function (err, result)
        {
            console.dir(result);
        })
    });

Guntur Sarwohadi added 2 commits October 12, 2013 09:22
….prototype.post method to req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event.
….prototype.post method to req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event.
@tonyspiro
Copy link
Contributor

Has this been added to the master yet? I'm running into the same issue. Thanks.

@tonyspiro
Copy link
Contributor

Nevermind I just copied your version and it works great. Thanks!

@benmj
Copy link

benmj commented Jun 7, 2014

+1

@aguidrevitch aguidrevitch merged commit 83668f5 into aguidrevitch:master Jun 8, 2014
@aguidrevitch
Copy link
Owner

Merged in manually in 0.1.2 and published to npmjs.org. Please let me know if the issue is solved.

@@ -62,6 +62,7 @@ module.exports = function (options) {
if (!--counter) {
_.each(files, function (fileInfo) {
this.initUrls(fileInfo);
fileInfo = _.extend(fileInfo, self.req.fields);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this fix in 0.1.4

@aguidrevitch
Copy link
Owner

I've committed the code into 0.1.5 to address your issue. Now, each event handler gets request and response objects in addition to fileInfo. In your case you can use something like:

upload.on("end", function (fileInfo, req, res)
    {
        var filemanager = upload.fileManager({
            targetDir: __dirname + "/public/userfiles/" + req.param('user_id'),
            targetUrl: "/userfiles/" + req.param('user_id')
        });
        filemanager.move(fileInfo.name, "", function (err, result)
        {
            console.dir(result);
        })
    });

Please let me know if that works for you

@ghost
Copy link

ghost commented Apr 1, 2017

Unless i'm missing something vital, then this isn't working at all. Regardless of what i do, then reg.params gives an empty object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants