How are files encoded in multipart form data?
See original GitHub issueHey 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:
- Created 4 years ago
- Comments:6
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.
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?