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
this.request.files no longer works under 1.0.0 pre #909
Comments
I forgot this was automatic before. I'll make it automatic again. In the mean time, add the connect bodyParser middleware manually like this: Typing on mobile so sorry for the syntax errors :-)
|
I can see the code for On pre3 this is what happens: |
It's only defined on the sever because it's connect middleware. Is that the issue?
|
Oh did it, I mixed up the versions of iron router, accidentally tried on 0.9.4. It does add but there is still another issue:
Perhaps its to do with the connect 3.0 deprecation of bodyParser? |
See also #914 |
I'm on version 1.0.0, trying to set the limit of bodyParser
But I'm getting an error.
The |
Hey @boulaycote - yes, |
So I'm guessing that It's not quite working as I thought. I am sending a file with to a My route is defined within
Within the |
I'm not sure. Is |
Oh right. So since |
/me isn't super on top of connect middleware but it sounds like you are on the right track.
|
Huhu. Ok when I find a solution I'll post it here. |
Allright, A simple solution is to use Busboy as middleware. And then do something like this:
Then in your
Of course, to use Busboy, you will have to
Hope this helps! |
@boulaycote Thanks for this, just been using it and it works well. In case this helps anyone else (and assuming this is the correct way of doing this) one alteration I made was to put the req.pipe within the if req.method block in case the route was hit with anything other than a POST request, in which case the code would error as busboy would be undefined: if (Meteor.isServer) {
var Busboy = Meteor.npmRequire("Busboy"),
fs = Npm.require("fs"),
os = Npm.require("os"),
path = Npm.require("path");
Router.onBeforeAction(function (req, res, next) {
var filenames = []; // Store filenames and then pass them to request.
if (req.method === "POST") {
var busboy = new Busboy({ headers: req.headers });
busboy.on("file", function (fieldname, file, filename, encoding, mimetype) {
var saveTo = path.join(os.tmpDir(), filename);
file.pipe(fs.createWriteStream(saveTo));
filenames.push(saveTo);
});
busboy.on("field", function(fieldname, value) {
req.body[fieldname] = value;
});
busboy.on("finish", function () {
// Pass filenames to request
req.filenames = filenames;
next();
});
// Pass request to busboy
req.pipe(busboy);
} else {
next();
}
});
} |
Thank you so much for this. works. |
Just posting this for completeness. If you want to pass your files together with the request or store them in mongodb, you can use this (beware that this stores them in memory though):
You can then store them in your db or somewhere else like this:
This exact example returns a link for the Froala WYSIWYG editor, but it's just an example. |
So is this right Iron:Router doesn't support reading directly Post data? |
hey guys - any update on this? Can we no access the file data from a form with Iron Router 1.0.8? |
The busboy solution worked perfectly for me. Note: Be careful with the capitalization of 'Busboy' - if you don't use the correct lower case Meteor.npmRequire("busboy"), it will work in dev, but fail on certain prod environments. There's a thread resolving this and I suspect this may be the source of the mistakes. |
So using busboy is a must ? |
Yeah. It seems like working quiet well using Busboy, |
@pmgration @boulaycote |
@mushkab My project is working well still. Could you specify your case? |
@nicejwjin when i handle http post in server (from third party application - webhook) with content type of multipart form data(parse it in router.onbeforeaction), i get req.body undefined in production environment. this does not happen in development where req.body is object({}). the post call is totally equal in both cases. busboy.on("field", function(fieldname, value) { wondering what causes this. |
@mushkab I had the exact problem as you, it seems that the body object does not exist at 'onBeforeAction' time. My quick work around the problem was to extend the request object with a new postData 'sub-object'. I did this with underscoresj which I was already using in my project. In the end this is working for me: if (Meteor.isServer) {
var Busboy = Meteor.npmRequire("busboy"),
fs = Npm.require("fs"),
os = Npm.require("os"),
path = Npm.require("path");
Router.onBeforeAction(function (req, res, next) {
var filenames = []; // Store filenames and then pass them to request.
_.extend(req, {postData: {}});
if (req.method === "POST") {
var busboy = new Busboy({ headers: req.headers });
busboy.on("file", function (fieldname, file, filename, encoding, mimetype) {
var saveTo = path.join(os.tmpDir(), filename);
file.pipe(fs.createWriteStream(saveTo));
filenames.push(saveTo);
});
busboy.on("field", function(fieldname, value) {
req.postData[fieldname] = value;
});
busboy.on("finish", function () {
// Pass filenames to request
req.filenames = filenames;
next();
});
// Pass request to busboy
req.pipe(busboy);
} else {
this.next();
}
});
} Then I can get access to the POST fields and their values using this in my route: this.request.postData |
it does not return any file name or data |
In 0.9.4 its possible to use
this.request.files
to retrieve any files uploaded to the server via a multipart POST request.I'm not sure whether it was intended but the same bunch of code no longer provides any files for the exact same requests with
iron:router@1.0.0-pre3
.If it was intentional is there a way around this?
The text was updated successfully, but these errors were encountered: