0

I am trying to save a Web API response which is in PDF in a PDF file.

Here is what I am doing:

FileStream fs = File.Create("API_response.pdf", 5120, FileOptions.None);
BinaryWriter F = new BinaryWriter(fs);
//ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba =  Encoding.Unicode.GetBytes(response.data);

F.Write(ba);
F.Close();
fs.Close();

But when I am trying to open the API_response.pdf file it says that file is damaged and could not be repaired.

This is the response I am getting from API and which I am dumping in API_response.pdf file,

%PDF-1.3
%âãÏÓ
1 0 obj<</Producer(htmldoc 1.8.27 Copyright 1997-2006 Easy Software Products, All Rights Reserved.)/CreationDate(D:20140725043937+0500)>>endobj
2 0 obj<</Type/Encoding/Differences[ 32/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quotesingle/parenleft/parenright/asterisk/plus/comma/hyphen/period/slash/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright/asciicircum/underscore/grave/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde 160/space/exclamdown/cent/sterling/currency/yen/brokenbar/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot/minus/registered/macron/degree/plusminus/twosuperior/threesuperior/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine/guillemotright/onequarter/onehalf/threequarters/questiondown/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis/yacute/thorn/ydieresis]>>endobj
3 0 obj<</Length1 1279/Length2 92942/Length3 544/Filter/FlateDecode/Length 93901     >>stream
xœ»s”dm³=X¶Ùå,WuÙ¶mÛ¶mÛ¶mÛ¶ÝÕe›]˜~¿û»ßoîÌ?³2WžóìˆØODœg­<¹’”P^‰FÀØÎÐDÔÎÖ™†–ž kacèâ$cg+M£hbæø²Â’*[8[›ü/+©ª‰£“…-çû 9š8ÿ„
œÿº+›»

Please let me know what I am missing?

6
  • 4
    What is response here? And why are you using a text encoding (ASCII) for binary data? Fundamentally, you should be avoiding doing anything with text here. Commented Jul 25, 2014 at 9:45
  • I am completely new to CSharp. I am not sure of using any specific encoding. Kindly correct me. Commented Jul 25, 2014 at 9:51
  • Hi @JonSkeet I have made some changes. Is this a right way to go? However I am not able to correct the failure! Commented Jul 25, 2014 at 10:00
  • 1
    No, absolutely not - it's still using a text encoding. You're dealing with binary data, not text. You shouldn't have a string at all. (If you're new to C#, I strongly suggest you start learning the core details of the language and the platform with console apps before trying "real" applications.) Commented Jul 25, 2014 at 10:06
  • What is the type of response.Data? Looking at the type of arguments, it seems to be a string or char[]. if its a HttpResponse, then get the Response stream and then try writing into a file. Commented Jul 25, 2014 at 10:20

1 Answer 1

1

BinaryWriter is for serialising data in a binary format. It's not for writing arbitrary data unchanged into a stream. In fact, a stream already can do that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.