简体   繁体   English

使用Javascript在content-disposition头中设置size参数

[英]Setting size parameter in content-disposition header with Javascript

The Problem 问题

I've been trying to set the size parameter in the content-disposition header for quite some time. 我一直在尝试在content-disposition头中设置size参数一段时间。

function uploadFile(file) {
    const data = new FormData();
    data.append('file', file);

    axios.post('url/of/endpoint', data);

FormData does not send the size parameter by default and, to my knowledge, I can't see a way of setting it myself. 默认情况下,FormData不会发送size参数,据我所知,我看不到自己设置它的方法。 Is there a way of doing this with Axios? 有没有办法用Axios做到这一点? Do I need to go more lower level and build my own headers? 我是否需要更低级别并构建自己的标题?

The Context 上下文

My primary objective is to pipe a file input stream via a Java endpoint to S3. 我的主要目标是通过Java端点将文件输入流传输到S3。 But without the file size, all of the data gets loaded into memory and has a high chance of causing an OOM exception. 但是没有文件大小,所有数据都会被加载到内存中,并且很有可能导致OOM异常。 This specific problem has been asked here as well . 这里也提到了这个具体问题。 Is writing my own byte buffer the only way? 是编写自己的字节缓冲区的唯一方法吗? Should I just stop being lazy and do that? 我应该停止懒惰吗? Seems like an overly-complex solution... 看起来像一个过于复杂的解决方案......

Is there a reason as to why the file size isn't normally included in FormData? 有没有理由说为什么文件大小通常不包含在FormData中? I've tried using Content-Length, but that includes the content-disposition header and leads to a mismatched byte count. 我尝试过使用Content-Length,但这包括内容处置标头,导致字节数不匹配。

Right, so after going around the office... then to my friends... I come to the conclusion that this is a Java shortcoming. 是的,所以在办公室周围...然后到我的朋友......我得出结论,这是Java的缺点。 Well, not the original question, but the fact that a multipart upload requires the total file size. 好吧,不是原始问题,而是分段上传需要总文件大小的事实。 So, the implementation I'm currently going with, which makes me feel super dirty, is adding a FormData item size on the client side. 因此,我目前正在使用的实现,这让我觉得非常脏,是在客户端添加FormData项目size

function uploadFile(file) {
    const data = new FormData();
    data.append('size', file.size.toString());
    data.append('file', file);

    axios.post('url/of/endpoint', data);

And then updating the backend to pass that size to the S3 SDK... ... oof. 然后更新后端以将该大小传递给S3 SDK ...... ... oof。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM