Browser support of multipart responses

Viewed 30245

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.

4 Answers

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!

Related