English 中文(简体)
Browser support of multipart responses
原标题:

I would like to create a HTTP response, using multipart/mixed, but I m not sure which browsers support it; and if it s as convenient as it sounds, from the client s point of view. To be honest, I do not need specifically that content type. I just want to transmit more than one file in the same response; maybe there s another content-type more used.

最佳回答

I ve tested it, with a home-made server and a simple response. Not sure if the response is well-formed because no browser understands it 100% OK. But here are the results:

  • Firefox 67.0.1 (64-bit): Renders only the last part, others are ignored.
  • IE 11.503: Saves all the content in a single file (including the boundaries), nothing is rendered.
  • Chrome May 2019: Saves all the content in a single file, nothing is rendered.
  • Safari 4: Saves all the content in a single file, nothing is rendered.
  • Opera 10.10: Something weird. Starts rendering the first part as plain/text, and then clears everything. The loading progress bar hangs on 31%.

Here s the complete response, if anyone finds any error, please tell me and I ll try again:

HTTP/1.1 200 OK
Date: Tue, 01 Dec 2009 23:27:30 GMT
Vary: Accept-Encoding,User-Agent
Content-Length: 681
Content-Type: Multipart/mixed; boundary="sample_boundary";

Multipart not supported :(
--sample_boundary
Content-Type: text/css; charset=utf-8
Content-Location: http://localhost:2080/file.css

body
{
 background-color: yellow;
}
--sample_boundary
Content-Type: application/x-javascript; charset=utf-8
Content-Location: http://localhost:2080/file.js

alert("Hello from a javascript!!!");

--sample_boundary
Content-Type: text/html; charset=utf-8
Content-Base: http://localhost:2080/

<html>
<head>
    <link rel="stylesheet" href="http://localhost:2080/file.css">
</head>
<body>
 Hello from a html
    <script type="text/javascript" src="http://localhost:2080/file.js"></script>
</body>
</html>
--sample_boundary--
问题回答

In my experience, multipart responses work in Firefox but not in Internet Explorer. This was 2 years ago, using the browsers of the time.

I have had HTTP multipart responses working for a stream of JPEG images. For example, Axis IP cameras use for their motion JPEG stream for Firefox. For Internet explorer, Axis require the use of a plugin.

If Firefox-only support meets your requirements, then I recommend setting the content-length header in each part of the multi-part response. It might help to make the boundary string identical in the original HTTP header and the multi-part response (the -- is missing in the HTTP header).

Two ideas:

  1. Formatting: I think "multipart" should be in lower case, and I don t think a semicolon is expected at the end of the Content-type header (although it s doubtful that it will make a difference, it s possible that it might).
  2. Have you tried replace mode? Just use: Content-type: multipart/x-mixed-replace -- everything else should stay the same.

 Multi part it yourself

(A good option)

A multipart response can be made manually!

So one can write a no multipart response! Let s say in chunked mode! There it make sense!

So you are streaming the data!

Send all as blunt text!

Make your own separators! Between each part!

In the browser! Extract and parse the data! Split to get each part separately!
And parse each appart! Depending on what type of data it hold!

So if a part is json! You parse it as so!

Quick illustration! Let say we want to send a csv file! Or some other type of file! Along that we want to send too a json object!

And that by streaming it by chunk

Here a code that illustrate that in express:

const data = {
    initCapital: fileState.mappingObj.initialCapital
};

res.write(JSON.stringify(data, undefined, 0));
res.write( >>>> ); // parts separator
fileState.readStream.pipe(res); // continue streaming the file (chunk by chunk)

And in the client

export function parseBackTestFetchedData(data: string) {
    const [_data, csvData] = data.split( >>>> );
    return {
        data: JSON.parse(_data),
        backTestStatesData: parseCsv(csvData)
    };
}

enter image description here

That way! it doesn t matter who the client is!





相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...

热门标签