.net - Byte array written to isolated storage area file in C# Windows Phone 7 app is invalid -
i have c# windows phone 7.1 app downloads pdf file foreign web server , (tries) save isolated storage area file. have tried several different ways done, file ends 30% large , when open in text editor, instead of seeing usual 'pdf' characters @ start of file followed encoded characters, see junk. test file i'm using supposed 161k when view file isolated storage explorer, it's 271k.
first download file string. inspected string @ point in debugger , contain proper values , correct length. trouble happens when try write isolated storage area. tried both streamwriter & binarywriter identical invalid results. contents of resulting file appears long stream of junk characters. note, deleting file if exists in case, before writing out contents. below code using binarywriter version. wrong?
async public static task urltofileasync( string strurl, string strdestfilename, iprogress<int> progress, cancellationtoken canceltoken) { strurl = strurl.trim(); if (string.isnullorwhitespace(strurl)) throw new argumentexception("(misc::urltofileasync) url empty."); strdestfilename = strdestfilename.trim(); if (string.isnullorwhitespace(strdestfilename)) throw new argumentexception("(misc::urltofileasync) destination file name empty."); // create isolated storage file. // filestream fs = misc.createisolatedstoragefilestream(strdestfilename); isolatedstoragefile isostorage = isolatedstoragefile.getuserstoreforapplication(); // delete file first. if (isostorage.fileexists(strdestfilename)) isostorage.deletefile(strdestfilename); isolatedstoragefilestream theisostream = isostorage.openfile(strdestfilename, filemode.create); filestream fs = theisostream; // if stream writer null, file not created. if (fs == null) throw new system.io.ioexception("(misc::urltofileasync) error creating or writing file named: " + strdestfilename); binarywriter bw = new binarywriter(fs); try { // call urltostringasync() web file string first. string strfilecontents = await urltostringasync(strurl, progress, canceltoken); // >>>> note: strfilecontents looks correct , correct size. // operation cancelled? if (!safecancellationcheck(canceltoken)) { // note. binarywriter not have async method take hit here // synchronous operation. // see stack overflow post. // http://stackoverflow.com/questions/10315316/asynchronous-binaryreader-and-binarywriter-in-net // >>>> note: strfilecontents.tochararray() looks correct , correct length. bw.write(strfilecontents.tochararray(), 0, strfilecontents.length); } // if (safecancellationcheck(canceltoken)) } { // make sure file cleaned up. bw.flush(); bw.close(); // make sure file disposed. bw.dispose(); } // try/finally // >>>> note: output file in isolated storage explorer wrong size , contains apparently junk. } // async public static void urltofileasync
you cannot download binary string. result not correct, have found out.
see answer, demonstrates how download binary file isolated storage: https://stackoverflow.com/a/6909201/1822514
Comments
Post a Comment