最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Save base64 encoded data sent over http as binary with browser - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

Base64 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.

发布评论

评论列表(0)

  1. 暂无评论