Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Unable to upload a file #202

Closed
LeviRosol opened this issue Mar 27, 2015 · 3 comments
Closed

Unable to upload a file #202

LeviRosol opened this issue Mar 27, 2015 · 3 comments

Comments

@LeviRosol
Copy link

I'm pulling my hair out on this one. I have a POST endpoint and have the spec setup like so:

module.exports.create = {
  'spec': {
    description : "Create a new Stock",
    path : "/stocks",
    method: "POST",
    summary : "Create a new Stock",
    notes : "Create a new Stock",
    type : "Stock",
    nickname : "postStock",
    produces : ["application/json"],
    parameters : [
      paramTypes.form("quantity", "how many are there?", "integer")
    ]
  },
  'action': function(req, res) {
    console.log("**1");
    console.log(req.body.quantity);
    console.log(req.files);
    console.log("**2");
    }
  };

When swagger-ui makes that post, all is dandy. I see quantity with a value and files as undefined. However, when i add the following param to the parameters collection:

      {
        "name": "image",
        "description": "The file to upload.",
        "paramType": "body",
        "required": true,
        "allowMultiple": false,
        "dataType": "file"
      }

both req.body.quantity and req.files are undefined.

I've explored a number of options for fixing this. Using type instead of dataType, File instead of file, and form instead of body, and nothing seems to work. I've also chased down the idea that my version of swagger-ui is different than what is included with this module, and still have the same issue.

Is this possibly related to the formData data type that I see the current Pet Store demo using?

Can anyone point me in the right direction on this one? Just seeing a working example with source would be awesome. As is, I don't think it's possible to upload a file, let alone a file along with other params.

@LeviRosol
Copy link
Author

FWIW, I am using the latest version of Express so I've pulled in the following modules manually (as compared to the Hello World issue here)

var bodyParser = require('body-parser');
var multer = require('multer')

@LeviRosol
Copy link
Author

Fantastic news! A fix has been found.

It appears my previous comment actually has a lot of worth. For me in particular the issue came down to correctly having multer required, but never setting the dest folder via:

app.use(multer({ dest: './uploads/'}));

Once that was set, req.files has data, and so do my other params.

In summary, I'm using express 4.12.2, and in order to get file uploads to work at all, let alone in swagger-ui, the multer module is needed, and a dest folder needs to be set.

I hope this issue being documented and the fix helps someone else!

@fehguy
Copy link
Contributor

fehguy commented Mar 27, 2015

@LeviRosol thanks for the persistence and sorry I couldn't help sooner. Will get this documented.

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

No branches or pull requests

2 participants