Download a File From a External sever through Asp.net & C# -
first,i uploaded file in external server , got url server.now want download uploaded file external server through url got server through asp.net , c#. wrote c# code download file, during process got exception "exception of type 'system.outofmemoryexception' thrown". following c# code wrote download:
double dlsize=0; string url = "http://www.sample.com/download.zip"; \\file size: 75mb int lastindex = url.lastindexof("/"); string tempurlname = url.substring(lastindex + 1, url.length - (lastindex + 1)); webrequest owebrequest = webrequest.create(url); owebrequest.timeout = -1; webresponse owebresponse = owebrequest.getresponse(); if (owebresponse.contenttype != "text/html; charset=utf-8") { string myfile = owebresponse.headers.get("content-length"); int tempmyfile = convert.toint32(myfile); double bytes_total = convert.todouble(tempmyfile); dlsize = convert.todouble(bytes_total / (1024 * 1024)); system.io.memorystream omemorystream = new memorystream(); byte[] buffer = new byte[4096]; using (system.io.stream ostream = owebresponse.getresponsestream()) { int count = 0; { count = ostream.read(buffer, 0, buffer.length); omemorystream.write(buffer, 0, count); } while (count != 0); } system.io.filestream ofilestream = new system.io.filestream("c:\documents , settings\arun\desktop\new folder\download.zip", system.io.filemode.create); omemorystream.writeto(ofilestream); ofilestream.flush(); ofilestream.close(); omemorystream.flush(); omemorystream.close(); owebresponse.close(); }
it easier this:
webclient webclient = new webclient(); webclient.downloadfile(remotefileurl, localfilename);
Comments
Post a Comment