0

How do I get the binary data of the buffer in C#?

item = {[_buffer, JVBERi0xLjMKJeLjz9MKMiAwIG9iago8PAovQ3JlYXRpb25EYXR]}

I tried this but get an error:

 HttpResponseMessage response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                var dataStream = response.Content.ReadAsStringAsync().Result;
                var parsed = JObject.Parse(dataStream);
                if (dataStream == null)
                    return HttpNotFound();

                foreach (dynamic item in parsed)
                {
                    // If user decides to save the file, this will help...
                    Response.AddHeader("content-disposition", "filename=" + Path.GetFileName(fileDoc));
                    return File(item._buffer, "application/pdf");
                }
            }
Scott
  • 2,456
  • 3
  • 32
  • 54
  • What JSON does the `dataStream` variable contain? – dbc Jan 29 '18 at 18:46
  • dataStream = "{\"_buffer\":\"JVBERi0xLjMKJeLjz9MKMiAwIG9iago8PAovQ3JlYXRpb25EYXRlIChEOjIwMTgwMTI2MjI0ODI1LTA1JzAwJykKL01vZERhdGUgKEQ6MjAxODAxMjYyMjQ4MjUtMDUnMDAnKQovUHJvZHVjZXIgKEJDTCBlYXN5UERGIDcuMDAgXCgwMzU1XCkpCi9DcmVhdG9yIChlYXN5UERGIFNESyA3IDcuMCkKPj4KZW5kb2JqCgo4I... – Scott Jan 29 '18 at 18:57
  • Your `dataStream` in your comment contains some odd Unicode characters such as [zero-width non-joiners](http://www.fileformat.info/info/unicode/char/200c/index.htm). Try to copy & paste the comment text into Visual Studio and you will see them. Are those really present? Can you [edit] your question to show the actual JSON the way e.g. [this one](https://stackoverflow.com/q/4749639/3744182) does? – dbc Jan 29 '18 at 19:09
  • This is my "parsed" from above: item={ [ _buffer, JVBERi0xLjMKJeLjz9MKMiAwIG9iag ] } – Scott Jan 29 '18 at 19:16
  • `{ [ _buffer, JVBERi0xLjMKJeLjz9MKMiAwIG9iag ] }` isn't valid JSON. Upload it to https://jsonlint.com/ and you will see an error. What I'm hoping for is to see an example of the "raw" JSON you are receiving so I can test a solution. Otherwise I could guess at a solution but it might or might not work. – dbc Jan 29 '18 at 19:21

1 Answers1

1

Assuming your raw JSON looks something like this:

{
  "_buffer": "JVBERi0xLjMKJeLjz9MKMiAwIG9iago8PAovQ3JlYXRpb25EYXRlIChEOjIwMTgwMTI2MjI0ODI1LTA1JzAwJykKL01vZERhdGUgKEQ6MjAxODAxMjYyMjQ4MjUtMDUnMDAnKQovUHJvZHVjZXIgKEJDTCBlYXN5UERGIDcuMDAgXCgwMzU1XCkpCi9DcmVhdG9yIChlYXN5UERGIFNESyA3IDcuMCkKPj4KZW5kb2JqCgo4"
}

Then the value of the "_buffer" property would appear to be Base64 encoded binary. As documented in its serialization guide Json.NET supports automatically deserializing Base64 strings to byte []. Thus you can do:

var _buffer = JsonConvert.DeserializeAnonymousType(dataStream, new { _buffer = (byte[])null })._buffer;

Then pass the returned byte array to Controller.File(Byte[], String).

By using JsonConvert.DeserializeAnonymousType() you avoid the need to load your (possibly large) response into an intermediate JToken hierarchy, and also avoid the need to create an explicit, concrete type just to deserialize a single byte [] property.

dbc
  • 104,963
  • 20
  • 228
  • 340