First,I uploaded a file in a external server and got a url from that server.Now i want to download that uploaded file from the external server through that url which i got from that server through asp.net and c#. I wrote a c# code to download that file, but during that process i got an exception "Exception of type 'System.OutOfMemoryException' was thrown". The following is the c# code which i wrote to 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;
do
{
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 and Settings\arun\Desktop\New Folder\download.zip", Sy开发者_JAVA技巧stem.IO.FileMode.Create);
oMemoryStream.WriteTo(oFileStream);
oFileStream.Flush();
oFileStream.Close();
oMemoryStream.Flush();
oMemoryStream.Close();
oWebResponse.Close();
}
It would be easier to do it like this:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
精彩评论