harryboy
6 years agoOccasional Contributor
Upload a file to server using NodeJS and Swagger
I want to upload a file to a server using Swagger. I have created the YAML to upload file as follows:
/Upload:
post:
summary: Uploads a file.
operationId: bulkUpload
consumes:
- multipart/form-data
parameters:
- in: formData
name: upfile
type: file
description: The file to upload.
responses:
200:
description: "File Uploaded"
x-swagger-router-controller: "Default"
My DefaultService.js file contaians the function:
var fs = require('fs');
exports.bulkUpload = function(args, res, next)
{
var stream = fs.createWriteStream("UploadedFile.txt");
stream.once('open', function ()
{
stream.write('blah blah'); //works - writes 'blah blah' to file
stream.write(JSON.stringify(args.upfile.value));//bombs out after a while
stream.write(args.upfile); //does not work
stream.write(args.upfile.value); //does not work
stream.end();
});
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
res.end(JSON.stringify('File Uploaded'));
}
I want to upload the original file without having to create my own test file. How can I do this?
You will see that I am trying to write the uploaded file to a test file "UploadedFile.txt". It is currently failing as you can see from my attempts.