Hello, this is more a doubt than a issue.
I'm using the method attach() to simulate a file upload to a express middleware. I was expecting that the request.files would be filled with the information of the attached file, but instead I'm receiving an undefined variable on request.
Am I doing something wrong?
request(app)
.post('/test')
.attach('image', __dirname + '/fixtures/fatcat.jpeg')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(201)
.end(function(err, res){
if (err) throw err;
done();
});
middleware
return function handler(req, res, next) {
console.log(req.files); // returns undefined :(
}
Thanks in advance.
Forgot to put the route declaration:
app.post('/test', uploadHandler, function(req, res){
res.send(201, { name: 'tobi' });
});
You probably didn't use the bodyParser() middleware in express.
Having same problem... let me know if you sorted it @wlepinski
Wow, this issue very old, @SteveNewhouse I really don't remember what I did so try what @gjohnson said. Good luck.
how to send image files array and with many fields?
@calidion
+1 how to send image files array and with many fields?
You can attach files just as OP described. The problem is that the multipart parser is no longer included in express bodyparser. You need to use a seperate package such as multer to handle multipart/form-data. I just found this out after upgraded my project to express 4.x.
my solution was to use.field()s along with .attach()
(if i remember correctly i even got a warning sometime that .send() and .attach() don't work together)
i've encountered so many issues with multer and multipart form data, so i'd rather handle base64 via json
my solution was to use
.field()s along with.attach()
(if i remember correctly i even got a warning sometime that.send()and.attach()don't work together)i've encountered so many issues with multer and multipart form data, so i'd rather handle
base64via json
return request
.post('/api/v1/client/1/progress_note')
.field('answers', progressNoteData.answers)
.field('duration', progressNoteData.duration)
.field('templateId', progressNoteData.templateId)
.field('service', progressNoteData.service)
.field('service_time', progressNoteData.service_time)
.attach('attachment', 'test/mock-data/sample-image.png')
@gjohnson
@SteveNewhouse
@radiumrasheed .filed only works if the field is a string, how can a field of an array or object be handled? I tried creating multiple fields with the same name, but throws error
@radiumrasheed
.filedonly works if the field is astring, how can a field of an array or object be handled? I tried creating multiple fields with the same name but throws an error
This works by stringify the field
yes, you need to stringify the array
Adding fields didn't seem to change how attachment was handled for me. I still wasn't able to see it when printing out req.files or req.file.
If someone is still looking for a solution, I used "form.parse" as part of multiparty library on my backend (nodejs) to get the files object from req, in which took me forever to realize that the file was stored on the attachment field on "files" as you can see below.
exports.nodeFunction = function (req, res) {
var form = new multiparty.Form({
uploadDir: 'app/tmp'
});
form.parse(req, function (err, fields, files) { // https://www.npmjs.com/package/multiparty
var file = files.file ? files.file[0] : files.attachment[0];
// when uploaded on front end, files.file has the file, but files.attachment holds the file when using supertest in mocha.
});
Here's my test function as well:
describe('POST', function () {
it('Should upload file', async function () {
const res = await request(app).post(url) // request = require('supertest')
.attach('attachment', path.resolve(__dirname, filename)) // using "path" library
.expect(200)
});
});
@gjohnson I have the updated body-parser for Express and it still does not work. This issue should be re-opened unless a solution is provided.
Most helpful comment