Dave New
Dave New

Reputation: 40002

Return binary string as plain text in browser window (and not as a downloadable file)

I want to return the following protobuf serialised binary data to the browser (Chrome) and not as a downloadable file. I don't understand the mechanism that is prompting a download. It is not the mime type as I am using text/plain elsewhere.

Controller:

[HttpGet]
public async Task<ActionResult<string>> GenerateProtoFeed()
{
    var feed = _gtfsrService.GenerateFeed();

    using (var stream = new MemoryStream())
    {
        feed.WriteTo(stream);

        stream.Position = 0;
        using (var reader = new StreamReader(stream))
        {
            return Content(reader.ReadToEnd(), "text/plain");
        }
    }
}

What I really want is this (example) to be returned in the browser window:

2.0?????/?
-Mcycmmp9-o4C0qeoGdz*?
????/*0
rE6s0CN800STv61PAKtfHAL6wS0jjmZkSZwq1PAKtf8A08Z?
?
?#StationAlert Elevators at Commercial-Broadway and Brentwood Stations are temporarily out of service today. ^sdken

Upvotes: 0

Views: 241

Answers (1)

Daan Reid
Daan Reid

Reputation: 361

The browser handles responses from a server differently depending on how the user has configured it, and on the mime type of the response.

It looks like your browser's default behaviour for text/plain is to prompt a save action. If you set the mime type of your response to text/html, the browser should simply display it.

Note that this is of course technically incorrect in this case.

Upvotes: 1

Related Questions