简体   繁体   English

如何提取通过 multipart/form-data 发送的文件的 Content-Type

[英]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.

相关问题 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? 使用Javascript(或Angular)在每个部分上使用不同的Content-Type编写multipart / form-data - Composing multipart/form-data with a different Content-Type on each parts with Javascript (or Angular) 在XHR中使用multipart / form-data作为Content-Type时获得'400 Bad Request' - Getting '400 Bad Request' when using multipart/form-data as Content-Type in XHR Axios multipart/form-data 出现在后端,没有内容类型 - Axios multipart/form-data comes up in the backend with no content-type 节点 axios 未发送正确的 header:'Content-Type':'multipart/form-data' - Node axios not sending correct header: 'Content-Type': 'multipart/form-data' 通过 multipart/form-data 内容类型上传文件时,在 Safari 中,预签名后上传到 S3 间歇性失败 - Pre-signed Post Uploads to S3 fails intermittently in Safari when uploading file via multipart/form-data content type 如何通过sails.js中的表单获取数组/对象(使用enctype multipart / form-data) - How to get arrays/objects sent via form in sails.js (with enctype multipart/form-data) axios 发布请求正在发送请求 header 的 Content-Type: multipart/form-data 导致未定义的 req.body - axios post request is sending a request header of Content-Type: multipart/form-data resulting in undefined req.body PHP + JS:如何以HTML形式将文件上传为Content-Type Multipart(通过JS)? - PHP+JS: How to do Fileuploads in HTML Form as Content-Type Multipart (via JS)? 客户端设置了multipart / form-data请求的文件部分的内容类型错误 - Content type for file part of the multipart/form-data request is set wrong by the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM