0

I call an API to get a PDF file. The API returns it as a string with binary data. Now I need to save it to a file without any conversion of the data. How can I do this in C#?

I have been trying

    string file = await service.GetDocumentsAsync(document.FileId);  //  Gets the filedata
    byte[] byteArray = file.Select (c => (byte)c).ToArray ();
    using (var stream = new FileStream($"c:\\temp\\{document.Id}.pdf", FileMode.Create))
    {
        stream.Write (byteArray,0,file.Length);
        stream.Close ();
    }

I do get the PDF, but it only has blank pages.

The beginning of the string when i look at it in the Debugger:

The beginning of the string when i look at it in the Debugger

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
smpet
  • 9
  • 2
  • 5
    _"a string with binary data"_ what does that mean? Base64-encoded? – Fildor Nov 14 '19 at 10:18
  • "string with binary data" like "0100101100" => [How To Write A String Of Binary To File C#](https://stackoverflow.com/questions/41778077/how-to-write-a-string-of-binary-to-file-c-sharp) ; if it look like "aGVsbG8gd29ybGQK" => [Base64 encoded string to file](https://stackoverflow.com/questions/1588306/base64-encoded-string-to-file) – Drag and Drop Nov 14 '19 at 10:20
  • What's wrong with `File.WriteAllText("C:\\...", file);`? – Longoon12000 Nov 14 '19 at 10:32
  • 1
    @Longoon12000 Because a PDF file will generally contain data which would be corrupted by treating it as a string (text) instead an treating it as bytes. – Andrew Morton Nov 14 '19 at 10:39
  • 1
    `service.GetDocumentsAsync` needs to give a `byte[]`, not a `string`. – Andrew Morton Nov 14 '19 at 10:41
  • If the data would get corrupted by being treated as a string then it would have been corrupted by the API. Just writing the string wouldn't corrupt it any further. Using `File.WriteAllText` should work in his case. – Longoon12000 Nov 14 '19 at 10:44
  • @smpet what you posted are the *binary* contents of a PDF file. Whatever `GetDocumentsAsync` is, it should have returned a `byte[]`. The data is already corrupted by the conversion from the actual `byte[]` response to `string`. You can't reverse it - all those � are replacement values returned when the code you used tried to convert those bytes into text and failed. – Panagiotis Kanavos Nov 14 '19 at 10:45
  • @smpet the problem is caused by the code inside `service.GetDocumentsAsync`. Perhaps it uses `HttpClient.GetStringAsync` when it should be using `GetStreamAsync` or `GetByteArrayAsync` and save the response to a file? Or perhaps it retrieves a response and uses `Content.ReadAsStringAsync()` without checking whether the content is a string or a binary file? – Panagiotis Kanavos Nov 14 '19 at 10:49
  • Do you have any idea how the file is converted and "sent as an string"? – dariogriffo Nov 14 '19 at 11:36
  • As suggested we had to change what the API returned. We use RestSharp and had to use Response.RawByte iso. Response. – smpet Nov 15 '19 at 09:45

1 Answers1

0

As suggested we had to change what the API returned. We use RestSharp and had to use Response.RawByte iso. Response.

smpet
  • 9
  • 2