c# - Copy a PDF Stream to File -
i'm calling routine in php (tcpdf) c# via webrequest using streamreader. pdf file returned stream , stored in string (obv). know data being returned string pdf file, i've tested in php. i'm having hard time writing string file , getting valid pdf in c#. know has way i'm trying encode file, several things i've tried have resulted in 'not today, padre' (i.e. didn't work)
here's class i'm using perform request (thanks user 'paramiliar' example i'm using/borrowed/stole):
public class httppostdata { webrequest request; webresponse response; public string senddata(string url, string postdata) { // create request url passed in paramaters request = (webrequest)webrequest.create(url); // set method post request.method = "post"; // set content type , content length request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = postdata.length; // convert post data byte array byte[] bytedata = encoding.utf8.getbytes(postdata); // request stream , write data stream datastream = request.getrequeststream(); datastream.write(bytedata, 0, bytedata.length); datastream.close(); // response response = request.getresponse(); datastream = response.getresponsestream(); streamreader reader = new streamreader(datastream); // read response string serverresponse = reader.readtoend(); //console.writeline(serverresponse); reader.close(); datastream.close(); response.close(); return serverresponse; } } // end class httppostdata
...and call it
httppostdata mypost = new httppostdata(); // postdata defined (not shown) string response = mypost.senddata("http://www.example.com/pdf.php", postdata);
in case isn't clear, i'm stuck writing string response
valid .pdf file. i've tried (thanks user adrian):
static public void savestreamtofile(string filefullpath, stream stream) { if (stream.length == 0) return; // create filestream object write stream file using (filestream filestream = system.io.file.create(filefullpath, (int)stream.length)) { // fill bytes[] array stream data byte[] bytesinstream = new byte[stream.length]; stream.read(bytesinstream, 0, (int)bytesinstream.length); // use filestream object write specified file filestream.write(bytesinstream, 0, bytesinstream.length); } } ..and call it: string location = "c:\\mylocation\\"; savestreamtofile(location, response); // <<-- throws error b/c 'response' string, not stream. new c# , having basic issues things
i think i'm close...a nudge in right direction appreciated.
you can use webclient. use method downloadfile, or async ones.
have fun! fernando.-
sorry, haven't read comments till now. guess have done this...
but may (just replace urls , paths) (from: http://msdn.microsoft.com/en-us/library/ez801hhe.aspx )
string remoteuri = "http://www.contoso.com/library/homepage/images/"; string filename = "ms-banner.gif", mystringwebresource = null; // create new webclient instance. webclient mywebclient = new webclient(); // concatenate domain web resource filename. mystringwebresource = remoteuri + filename; console.writeline("downloading file \"{0}\" \"{1}\" .......\n\n", filename, mystringwebresource); // download web resource , save current filesystem folder. mywebclient.downloadfile(mystringwebresource,filename); console.writeline("successfully downloaded file \"{0}\" \"{1}\"", filename, mystringwebresource); console.writeline("\ndownloaded file saved in following file system folder:\n\t" + application.startuppath);
Comments
Post a Comment