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.