question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How are files encoded in multipart form data?

See original GitHub issue

Hey all,

Thanks for your great work on FilePond! Really enjoying it so far.

One quick question about the library internals that seems like it might be more appropriate for GitHub than SO:

I’m trying to write a custom processor backend following the docs in order to upload files to Google Drive. However, I’m running into some strange behavior related to the file encoding that I don’t quite understand.

How are files encoded in multipart form data? I’m parsing the form data in a Lambda function with busboy and raw-body like so:

const parse = (event) => {
  return new Promise((resolve, reject) => {
    const contentType = event.headers['Content-Type'] || event.headers['content-type'];
    const bb = new busboy({headers: {'content-type': contentType}});
    const filePromises = []
    let data = {fields: {}, files: {}}

    bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
      data.files[fieldname] = {filename, encoding, mimetype}
      filePromises.push(
        getRawBody(file).then(rawFile => {data.files[fieldname].content = rawFile})
      )
    })
    .on('field', (fieldname, val) => {
      data.fields[fieldname] = val
    })
    .on('finish', () => {
      resolve(Promise.all(filePromises).then(() => data))
    })
    .on('error', err => {
      reject(err)
    })

    bb.end(event.body)
  })
}

Then, I pass the file data along to the Google Drive API using the buffer stored on data.files[fieldname].content. When I test this with a jpeg and retrieve the file from Google Drive, however, it appears to be encoded improperly, and I can’t open it as an image. The same behavior happens if I write data.files[fieldname].content to the filesystem with fs.writeFileSync and then open it locally, so the encoding problem isn’t happening with the Drive API.

How should I expect to handle the file data in the multipart form? Is it encoded in any particular way?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jeancochranecommented, Jul 15, 2019

Great, thanks! I’ll check whether Lamdba supports array buffers before copying the code from the file encode plugin.

Closing this since there was no underlying issue with the library. Thanks again for your support, I really appreciate it.

0reactions
rikschenninkcommented, Jul 14, 2019

Yes the file encode plugin is mainly there to allow for plain form submits. I think copy pasting the code inside the file encode plugin to your custom process method would be the best way to go.

I’m not sure if this is an option but maybe the remote service accepts array buffers?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does enctype='multipart/form-data' mean? - Stack Overflow
enctype(ENCode TYPE) attribute specifies how the form-data should be encoded when submitting it to the server ...
Read more >
Define multipart form data - GeeksforGeeks
The encoding process is performed before data is sent to the server as spaces are converted to (+) symbol and non-alphanumeric characters or ......
Read more >
RFC 2388 multipart/form-data - IETF
Each part may be encoded and the "content-transfer-encoding" header supplied if the value of that part does not conform to the default encoding....
Read more >
html - URL Encoded and Multipart Forms
application/x-www-form-urlencoded - Represents a URL encoded form. · multipart/form-data - Represents a Multipart form. · text/plain - A new form ...
Read more >
Encoding problems when using multipart/form-data - Forums
I have a form that allows file uploads, but the form also contains some plain text input fields. The problem is, when the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found