如何使用前端javascript将HTML5 FormData对象转换为原始有效负载?

如何使用前端javascript将HTML5 FormData对象转换为原始有效负载?,javascript,html,npm,ecmascript-6,multipartform-data,Javascript,Html,Npm,Ecmascript 6,Multipartform Data,在浏览器端脚本中,我希望获得整个POST请求有效负载流 但是,当主体是FormData对象时,特别是当它包含文件blob时,有没有简单的方法来创建它 这样做的原因是我想使用axios请求拦截器对整个请求体进行AES加密 例如: 我要转换FormData对象: const fd = new FormData() fd.append('example.png', file) // here file is a file object from the file input object 分为以下内

在浏览器端脚本中,我希望获得整个POST请求有效负载流

但是,当主体是FormData对象时,特别是当它包含文件blob时,有没有简单的方法来创建它

这样做的原因是我想使用axios请求拦截器对整个请求体进行AES加密

例如:

我要转换FormData对象:

const fd = new FormData()
fd.append('example.png', file) // here file is a file object from the file input object
分为以下内容:

------WebKitFormBoundaryMV9GYQ2pcwRJ6XAA
Content-Disposition: form-data; name="image"; filename="example.png"
Content-Type: image/png
<<blob bytes>>

------WebKitFormBoundaryMV9GYQ2pcwRJ6XAA--
----WebKitFormBoundaryMV9GYQ2pcwRJ6XAA
内容配置:表单数据;name=“image”;filename=“example.png”
内容类型:图像/png
------WebKitFormBoundaryMV9GYQ2pcwRJ6XAA--

有没有简单的方法来制作它或任何现有的npm包?

我不确定它是否真的能帮助您实现最终目标,但根据您的要求,您可以从这个FormData创建一个新对象,并将其作为纯文本使用:

(异步()=>{
const file=wait new Promise((res)=>document.createElement('canvas').toBlob(res));
const fd=new FormData();
append('example.png',file,'example.png');
const resp=新响应(fd);
log(wait resp.text());

})();这是否回答了您的问题?谢谢,但我认为如果我自己执行整个程序,可能有点不礼貌。我更希望有一个图书馆能做到这一点。