Wednesday, 15 June 2011

How to upload a file on server in c# in fast way -



How to upload a file on server in c# in fast way -

httpwebrequest request = (httpwebrequest)webrequest.create("request_uri_string"); filestream filestream = new filestream("path_to_my_file", filemode.open, fileaccess.read); stream requeststream = request.getrequeststream(); byte[] buffer = new byte[checked((uint)math.min(4096, (int)filestream.length))]; int bytesread = 1; while (bytesread != 0) { bytesread = filestream.read(buffer, 0, buffer.length); if (bytesread > 0) { requeststream.write(buffer, 0, bytesread); } } request.close(); filestream .close();

currently using above code.

is there other approach improve (means fast) this?

not sure faster, lot more readable:

using (var webclient = new webclient()) { webclient.downloadfile(remotefileurl, localfilename); }

edit:

since webclient idisposable, should disposed (i added using).

msdn says:

this method uses retr command download ftp resource. http resource, get method used.

c#

No comments:

Post a Comment