Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

How do I simulate a file upload? #87

Closed
ivanguimam opened this issue Mar 21, 2018 · 3 comments
Closed

How do I simulate a file upload? #87

ivanguimam opened this issue Mar 21, 2018 · 3 comments

Comments

@ivanguimam
Copy link
Contributor

I need to simulate a file upload and return the file name.

Only that sometimes it works and sometimes it generates a mistake

dyson.js

const DYSON = require('dyson')
const PATH = require('path')

let options = {
    configDir: PATH.join(__dirname, 'out-tsc', 'mock'),
    port: 3333
}
const configs = DYSON.getConfigurations(options);
const appBefore = DYSON.createServer(options);
const appAfter = DYSON.registerServices(appBefore, options, configs)

console.log('DYSON STARTED PORT 3333')

upload.ts

const formidable = require('formidable')

module.exports = {
    path: `/company/:id/file`,
    method: 'POST',
    render: async (req:any, res:any) => {
        try {
            let url = await upload(req)
            res.status(200).send({ url })
        } catch(e) {
            res.status(500).send(e)
       }
   }
}

function upload(req:any) {
    let form = new formidable.IncomingForm()

   return new Promise((resolve, rej) => {
       form.parse(req, (err, fields, files) => {
           if (files.file) resolve(files.file.name)
           else rej(err)
       })
   })
}

error

Error: MultipartParser.end(): stream ended unexpectedly: state = START
at MultipartParser.end (/home/ivan/repositories/luva_acessoria/frontend/node_modules/formidable/lib/multipart_parser.js:326:12)
at IncomingMessage.<anonymous> (/home/ivan/repositories/luva_acessoria/frontend/node_modules/formidable/lib/incoming_form.js:132:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
@ivanguimam
Copy link
Contributor Author

Suggestion:
Add in package.json: express-fileupload

Add in file lib/dyson.js

const fileUpload = require('express-fileupload')
...
const installMiddleware = (app, options) => {
    ...
    app.use(fileUpload())
    ...
}
...

@ivanguimam
Copy link
Contributor Author

PR: #88

@webpro
Copy link
Owner

webpro commented Jun 8, 2021

Thanks @ivanguimam! Perhaps a bit later than expected, but your PR has been merged and released in v4.1.0

@webpro webpro closed this as completed Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants