[英]How to extract the Content-Type of a file sent via multipart/form-data
I receive a Request in my cloudflare worker and want to upload the data to Google cloud storage.我在我的 cloudflare 工作器中收到一个请求,并希望将数据上传到 Google 云存储。 My problem is that I can't extract the content type from the multipart/form-data data I receive in order to upload it with the correct content type to GCS.我的问题是我无法从收到的 multipart/form-data 数据中提取内容类型,以便使用正确的内容类型将其上传到 GCS。
When I read the request with await req.formData()
I can get('file')
from the formData and it returns the raw file data that I need for the GCS, but I can't seem to see anywhere the file content-type that I need (I can see it only when looking at the raw Request body).当我使用await req.formData()
读取请求时,我可以从 formData 中get('file')
并返回 GCS 所需的原始文件数据,但我似乎无法在任何地方看到文件内容 -我需要的类型(我只有在查看原始请求正文时才能看到它)。
Here is my (striped down) code :这是我的(精简)代码:
event.respondWith((async () => {
const req = event.request
const formData = await req.formData()
const file = formData.get('file')
const filename = formData.get('filename')
const oauth = await getGoogleOAuth()
const gcsOptions = {
method: 'PUT',
headers: {
Authorization: oauth.token_type + ' ' + oauth.access_token,
'Content-Type': 'application/octet-stream' //this should by `'Content-Type': file.type`
},
body: file,
}
const gcsRes = await fetch(
`https://storage.googleapis.com/***-media/${filename}`,
gcsOptions,
)
if (gcsRes.status === 200) {
return new Response(JSON.stringify({filename}), gcsRes)
} else {
return new Response('Internal Server Error', {status: 500, statusText: 'Internal Server Error'})
}
})())
Reminder - the code is part of our cloudflare worker code.提醒 - 该代码是我们的 cloudflare 工作器代码的一部分。
It seems to me this should be straight forward, determining the type of file you extract from the multipart/form-data data.在我看来,这应该是直截了当的,确定您从 multipart/form-data 数据中提取的文件类型。 Am I missing something?我错过了什么吗?
Unfortunately, as of this writing, the Cloudflare Workers implementation of FormData is incomplete and does not permit extracting the Content-Type.不幸的是,在撰写本文时,FormData 的 Cloudflare Workers 实现尚不完整,并且不允许提取 Content-Type。 In fact, it appears our implementation currently interprets all entries as text and return strings, which means binary content will be corrupted.事实上,我们的实现目前似乎将所有条目解释为文本并返回字符串,这意味着二进制内容将被破坏。 This is a bug which will require care to fix since we don't want to break already-deployed scripts that might rely on the buggy behavior.这是一个需要小心修复的错误,因为我们不想破坏可能依赖于错误行为的已经部署的脚本。
Thanks Kenton for your response.感谢肯顿的回复。
What I ended up doing:我最终做了什么:
As the Cloudflare Workers don't support the multipart/form-data
of Blob or any type other than String, I ended up using the raw bytes in the ArrayBuffer
data type.由于 Cloudflare Workers 不支持 Blob 的multipart/form-data
或 String 以外的任何类型,我最终使用了ArrayBuffer
数据类型中的原始字节。 After converting it to an Uint8Array
I parsed it byte by byte to determine the file type and the start and end indexes of the file data.将其转换为Uint8Array
我逐字节解析它以确定文件类型以及文件数据的开始和结束索引。 Once I found the start and end of the transferred file I was able create an array of the file data, add it to the request and send it to the GCS as I showed above.找到传输文件的开头和结尾后,我就可以创建文件数据数组,将其添加到请求中并将其发送到 GCS,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.