c# - Send parts of a byte[] by webclient to create a large file in server side -
so trying transfer large byte[] chose separate in chunks of 20mb, , in first chunk received create file , add that,the rest open existing file , add remaining.the problem having instead of send first part , reconnect receive second part establishing 2 connections , sending 2 chunks @ same time.. how can send second after first finished?
client.openwriteasync(ub.uri); void client_openwritecompleted(object sender, openwritecompletedeventargs e) { if (e.cancelled) { messagebox.show("cancelled"); } else if (e.error != null) { messagebox.show("deu erro"); } else { try { using (stream output = e.result) { int countbytes; //for (int = 0; < max; i++) //{ if ( (max+1) != maxaux) { countbytes = zippedmemorystream.read(partofdataset, 0 , 20000000);//maxaux * 20000000 output.write(partofdataset, 0, (int)countbytes); if (max != maxaux) { client.openwriteasync(ub.uri); } maxaux++; } //} //numeroimagem++; } } catch (exception ex) { messagebox.show(ex.message); //throw; } } }
public void processrequest(httpcontext context) { //context.response.contenttype = "text/plain"; //context.response.write("hello world"); string imagename = context.request.querystring["imagename"]; string uploadpath = context.server.mappath("~/serverimages/"); byte[] bytes = new byte[20000000]; int bytestoread = 0; if (!file.exists(uploadpath + imagename)) { using (filestream stream = file.create(uploadpath + imagename)) { try { //list<byte> bytes = new list<byte>(); while ((bytestoread = context.request.inputstream.read(bytes, 0, bytes.length)) != 0) //context.request.inputstream.read(bytes, 0, 200000)) != 0) { stream.write(bytes, 0, bytestoread); stream.close(); } bytes = null; } catch (exception ex) { string error = ex.message; throw; } } } else { using (filestream stream = file.open(uploadpath + imagename,filemode.append)) { try { while ((bytestoread = context.request.inputstream.read(bytes, 0, bytes.length)) != 0) { stream.write(bytes, 0, bytestoread); stream.close(); } bytes = null; } catch (exception ex) { string error = ex.message; throw; } } } } public bool isreusable { { return false; } }
in fact gain nothing opening client again. can loop on zippedmemorystream in chunks , write output stream. data arrive in order on server side.
otherwise uploaddata methods, if want create new connection each time.
Comments
Post a Comment