Skip to content

How can I keep a multipart form upload in memory (skip the file)? #46

@jcalfee

Description

@jcalfee

I need to parse many requests with images and forward them to another server. Is there a way to avoid writing each one to a file? I would rather access some sort of Buffer with the file's data in it. Here is an example request:

curl -F "image=@$HOME/Pictures/blue_red_pill.jpg" -F "username=slim" http://localhost:3234/upload

Here is what I have so far:

const form = new formidable.IncomingForm()
const koaBody = require('koa-better-body')({
    multipart: true,
    IncomingForm: form,
})

form.onPart = function(part) {
    if (!part.filename) {
        // let formidable handle all non-file parts
        form.handlePart(part)
        return
    }   

    part.on('data', buffer => {
        if (buffer.length == 0) return
        console.log('received', buffer.length, 'bytes');
        // form.handlePart(???
    })

    part.on('end', () => {
        console.log('end');
        // form.handlePart(???
    })
    // part.on('error' // what about cleanup on error?
}

const router = require('koa-router')()
router.post('/upload', koaBody, function *() {
    const buffer = this.request[???]
})

I'm receiving the data, but I don't have anywhere to store it. Is there a way to get the data I collected back into the request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pkg: koa-better-bodyPriority: HighAfter critical issues are fixed, these should be dealt with before any further issues.Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.Status: AvailableNo one has claimed for resolving this issue. Generally applied to bugs and enhancement issues.Status: BlockedAnother issue needs to be resolved first or an external blocker.Type: EnhancementMost issues will probably be for additions or changes. Expected that this will result in a PR.Type: QuestionA query or seeking clarification. Probably doesn't need the attention of everyone.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions