KeystoneJS file upload not working in my app

夙愿已清 提交于 2019-12-24 14:51:11

问题


We are working with a client, that uses Keystonejs 0.3.16, on providing the possibility to upload files to the server file system (Not from the keystone admin ui). At first hand, this seemed trivial and I expected this to be working in less than an hour. After having worked on this until after midnight, motivated by the fact that I will sleep much better once this is sorted, I had to resign for the time being and indeed, my dreams where unsettled as a consequence. What a geek you must think, get a life! Yet, I am sure lots of people will know the feeling. I now turn to you, community and keystonejs authors, to help me understand what I am missing here.

I am using a very recent version of keystones, and I tried multiple libraries to handle the multipart format file upload as a middleware component. Multer, formidable, jfum, jquery-file-upload-middleware, connect-busboy, connect-multiparty... I did them all. One did not even install due to some compilation errors due to incompatible libraries, some gave me some 500 errors telling me that req.status is not defined, and all others telling me that upload went fine, but req.body and req.file stayed empty and nothing got downloaded.

I even added some logging to multer lib “makeMiddleware”, to see if files get parsed there, but the busboy.on('file', …) event never get’s fired, indicating no file is present in the body.

I haven’t tried this in a basic Nodejs Express server app yet, only on keystone js app, so maybe keystone pre-processes post requests somehow that would explain this phenomenon, ex. stripping the file content before it reaches my middleware?

Here is my basic form:

<form method="post" role="form" enctype="multipart/form-data" action="/bo/lib/upload">
<input type="file" id="imageFileId" name=" imageFileName">
<input type="submit" value="Upload Image">
<form>

And here my example using Multer for instance:

var upload = multer({ dest: './uploads/'});

exports = module.exports = function(app) {
    app.post('/', upload.single('myimage'), function(req, res){
        console.log(req.body) // returns empty ???
        console.log(req.file) // returns empty ???
        res.status(204).end()
    }]);
};

If someone, maybe someone on the keystonejs dev side, could help me understand this, or even better, solve this issue, I would GREATLY appreciate your help, and for sure sleep much better tonight ;-)


回答1:


Having the same problem. Keystone is parsing the req using bodyParser before it ever gets to the ./routes/index.js where you have multer (or busboy in my case) attempting to parse the request. Disabling app.use(bodyParser.json()) and app.use(bodyParser.urlencoded()) in ./routes/index.js does not resolve the issue.

This is confirmed by the fact if you console.log(req.body) it is populated, but adding an event listener which calls console.log("Multer of busboy just parsed a file or field") on multer or busboy's parsing of a field or file will never output.



来源:https://stackoverflow.com/questions/35454860/keystonejs-file-upload-not-working-in-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!