admin管理员组文章数量:1390707
I'm writing a REST service that allows the receipt of a binary file resource.
I'm using RESTEXPRESS and I send the binary data as encoded base64 string over HTTP.
I have a bination of headers that allow the file to be received as an attachment but the browser does not decode the base64 back to binary.
Is my goal possible? To send arbitrary binary fileformat over http and have the browser decode from base64 to binary automatically and save the file. Or have the browser trigger an application associated with file-extension? Can I do this transparently Or do I have to use javascript?
Code showing the headers I use is listed below.
public String read(Request request, Response response) throws IOException
{
response.setIsSerialized(false);
response.setContentType("application/arbitraryBinaryFileFormat");
response.addHeader("Content-Transfer-Encoding", "Base64");
response.addHeader("Content-Encoding", "Base64");
response.addHeader("Content-Disposition","attachment; filename=r.arbitraryBinaryFileFormat");
String result = encodeFileToBase64Binary("/Users/xxx/Downloads/r.arbitraryBinaryFileFormat");
response.setBody(result);
return result;
}
I'm writing a REST service that allows the receipt of a binary file resource.
I'm using RESTEXPRESS and I send the binary data as encoded base64 string over HTTP.
I have a bination of headers that allow the file to be received as an attachment but the browser does not decode the base64 back to binary.
Is my goal possible? To send arbitrary binary fileformat over http and have the browser decode from base64 to binary automatically and save the file. Or have the browser trigger an application associated with file-extension? Can I do this transparently Or do I have to use javascript?
Code showing the headers I use is listed below.
public String read(Request request, Response response) throws IOException
{
response.setIsSerialized(false);
response.setContentType("application/arbitraryBinaryFileFormat");
response.addHeader("Content-Transfer-Encoding", "Base64");
response.addHeader("Content-Encoding", "Base64");
response.addHeader("Content-Disposition","attachment; filename=r.arbitraryBinaryFileFormat");
String result = encodeFileToBase64Binary("/Users/xxx/Downloads/r.arbitraryBinaryFileFormat");
response.setBody(result);
return result;
}
Share
Improve this question
asked Mar 5, 2014 at 18:00
user3384752user3384752
111 silver badge2 bronze badges
1 Answer
Reset to default 5Base64
is not a valid HTTP Content-Encoding
value. See the official list of registered encodings on IANA.
Content-Transfer-Encoding
is a MIME header, it is not used with HTTP at all (see RFC 2616 Section 19.4.5).
In short, HTTP has no concept of base64, so there is no way to force an HTTP client to auto-decode base64 encoded data. You should be sending the file data in its raw binary format, not encoding it as base64 at all. You can pass a byte[]
array instead of a string
to response.setBody()
, for instance.
As for invoking an application based on filename, using a Content-Disposition: attachment; filename="..."
header is the correct solution for that. You should be setting the Content-Type
to a real legitimate value that is appropriate for the file extension, or use application/octet-stream
instead.
本文标签: javascriptSave base64 encoded data sent over http as binary with browserStack Overflow
版权声明:本文标题:javascript - Save base64 encoded data sent over http as binary with browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744632772a2616640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论