javascript 如何将二进制多部分表单数据作为 base64 发送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11149930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:12:40  来源:igfitidea点击:

How to send binary multipart formdata as base64?

javascriptjqueryhtmlwicket

提问by David

Possible Duplicate:
How can you encode to Base64 using Javascript?

可能的重复:
如何使用 Javascript 编码为 Base64?

I have a web application based on Java, Wicket and JQuery which has a function to enable users to upload files (images, pdfs, rtf) through multipart/form-data.

我有一个基于 Java、Wicket 和 JQuery 的 Web 应用程序,它具有使用户能够通过 multipart/form-data 上传文件(图像、pdf、rtf)的功能。

Our web security infrastructure does filtering of all http traffic to pick up possibly malicious content e.g. XSS attacks, SQL injection, buffer overruns etc

我们的网络安全基础设施对所有 http 流量进行过滤以获取可能的恶意内容,例如 XSS 攻击、SQL 注入、缓冲区溢出等

The filter doesn't distinguish between normal text input fields and file data so it picks up false positives from many uploaded binary files, preventing those files from being uploaded. I cannot change the security policy.

过滤器不区分普通文本输入字段和文件数据,因此它会从许多上传的二进制文件中提取误报,防止上传这些文件。我无法更改安全策略。

It seems the best way around this would be for file data to be base-64 encoded so that it is sent with a Content-Transfer-Encoding:base64 similar to what email clients do.

似乎解决此问题的最佳方法是将文件数据进行 base-64 编码,以便使用类似于电子邮件客户端所做的 Content-Transfer-Encoding:base64 发送。

Is there any way to direct the browser to transfer binary as base64 or some other non-binary format?

有什么方法可以让浏览器将二进制文件作为 base64 或其他非二进制格式传输?

If not, could it be done manually with some JavaScript?

如果没有,是否可以使用一些 JavaScript 手动完成?

回答by didxga

It seems that you can set Content-Transfer-Encoding: base64
quote from W3C:

似乎您可以Content-Transfer-Encoding: base64
从 W3C设置报价:

Each part may be encoded and the "Content-Transfer-Encoding" header supplied if the value of that part does not conform to the default (7BIT) encoding

如果该部分的值不符合默认 (7BIT) 编码,则可以对每个部分进行编码并提供“Content-Transfer-Encoding”标头

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

Provided your Java Web Server in use doesn't ingore parameter Content-Transfer-Encoding.

如果您使用的 Java Web Server 不包含参数Content-Transfer-Encoding