使用 rackspace 形式的内容类型不正确 post

Incorrect content type using rackspace form post

我正在使用表单 post 方法将一些文件 post 到我在机架空间上的一个容器中。问题是,如果我先上传 pdf,然后一次性上传 png,它会尝试在 pdf 查看器中打开 png。检查后我才知道“Content-type”被设置为“application/pdf” for png.

我的表格是这样的

<form id="form" accept-charset="utf-8" action="action" method="POST" enctype="multipart/form-data" novalidate="novalidate">
<input type="hidden" name="redirect" value="rediredt url">
<input type="hidden" value="" name="max_file_size">
<input type="hidden" value="" name="max_file_count">
<input type="hidden" value="" name="expires" id="rack_expire">
<input type="hidden" value="" name="signature" id="rack_signature">

<div class="contentRow fileUploadWrap">
<p><input id="file2" type="file" name="file_1"></p>
<p><input id="file2" type="file" name="file_2"></p>
<p><input id="file3" type="file" name="file_3"></p>
<p><input id="file4" type="file" name="file_4"></p>
<p><input id="file5" type="file" name="file_5"></p>
<div class="contentRow">
<div class="submit"><input id="uploadFilesBtn" class="btn btn--primary" type="submit" value="Upload"></div> </div>
</div>

</form>

我找不到为正在上传的文件设置内容类型的方法。有什么办法可以解决这个问题吗?

使用 append 而不是 input 并像这样指定文件名

formData.append('userfile[]', myFileInput.files[0], 'doc1.pdf');
formData.append('userfile[]', myFileInput.files[1], 'image1.png');

浏览器将识别文件类型(content-type)并相应地打开文件,结果可能因浏览器而异,但对于常见的文件格式,您可以依赖它。

FormData Documentation

中所述

If you specify a Blob as the data to append to the FormData object, the filename that will be reported to the server in the "Content-Disposition" header used to vary from browser to browser.

我找到的唯一解决方法是使用 api 方法 mentioned here.

更新上传文件的内容类型

我已经保存了文件名,所以我只是做了一个 post CURL 来更新信息并解决了这个问题。