Async download file in c# -
i need improve code not understand made mistake. file downloaded in corrupted format. using cookies required part of method.
/*downlod task */ public static async task<int> createdownloadtask(string urltodownload,string sdestinationpath, string cookiedstr) { int buffersize = 4096; int receivedbytes = 0; int totalbytes = 0; webclient client = new webclient(); client.headers.add(httprequestheader.cookie, cookiedstr); using (var stream = await client.openreadtaskasync(urltodownload)) { using (memorystream ms = new memorystream()) { int read = 0; totalbytes = int32.parse(client.responseheaders[httpresponseheader.contentlength]); var buffer = new byte[buffersize]; while ((read = await stream.readasync(buffer, 0, buffer.length)) > 0) { ms.write(buffer, 0, read); filestream file = new filestream(sdestinationpath, filemode.append, fileaccess.write); ms.position = 0; ms.writeto(file); file.flush(); file.close(); buffer = new byte[buffersize]; receivedbytes += read; console.writeline(receivedbytes + " " + totalbytes); // downloadbytesprogress args = new downloadbytesprogress(urltodownload, receivedbytes, totalbytes); } ms.close(); } return receivedbytes; } }
Comments
Post a Comment