public static bool DownloadFromWeb(string url, string file, string targetFolder) { try { WebResponse response = WebRequest.Create(url + file).GetResponse(); Stream responseStream = response.GetResponseStream(); byte[] buffer = new byte[0x400]; int contentLength = (int) response.ContentLength; ByteArgs args2 = new ByteArgs { Downloaded = 0, Total = contentLength }; ByteArgs e = args2; if (BytesDownloaded != null) { BytesDownloaded(e); } MemoryStream stream2 = new MemoryStream(); while (true) { while (responseStream == null) { } int count = responseStream.Read(buffer, 0, buffer.Length); if (count == 0) { e.Downloaded = contentLength; e.Total = contentLength; if (BytesDownloaded != null) { BytesDownloaded(e); } break; } stream2.Write(buffer, 0, count); e.Downloaded = count; e.Total = contentLength; if (BytesDownloaded != null) { BytesDownloaded(e); } } byte[] buffer2 = stream2.ToArray(); responseStream.Close(); stream2.Close(); FileStream stream3 = new FileStream(targetFolder + file, FileMode.Create); stream3.Write(buffer2, 0, buffer2.Length); stream3.Close(); return true; } catch (Exception) { return false; } }
public static bool DownloadFromWeb(string url, string file, string targetFolder) { try { WebResponse response = WebRequest.Create(url + file).GetResponse(); Stream responseStream = response.GetResponseStream(); byte[] buffer = new byte[0x400]; int contentLength = (int)response.ContentLength; ByteArgs args2 = new ByteArgs { Downloaded = 0, Total = contentLength }; ByteArgs e = args2; if (BytesDownloaded != null) { BytesDownloaded(e); } MemoryStream stream2 = new MemoryStream(); while (true) { while (responseStream == null) { } int count = responseStream.Read(buffer, 0, buffer.Length); if (count == 0) { e.Downloaded = contentLength; e.Total = contentLength; if (BytesDownloaded != null) { BytesDownloaded(e); } break; } stream2.Write(buffer, 0, count); e.Downloaded = count; e.Total = contentLength; if (BytesDownloaded != null) { BytesDownloaded(e); } } byte[] buffer2 = stream2.ToArray(); responseStream.Close(); stream2.Close(); FileStream stream3 = new FileStream(targetFolder + file, FileMode.Create); stream3.Write(buffer2, 0, buffer2.Length); stream3.Close(); return(true); } catch (Exception) { return(false); } }
public static bool DownloadFromWeb(string url, string file, string targetFolder) { try { //open a data stream from the supplied URL WebRequest webReq = WebRequest.Create(url + file); var webResponse = webReq.GetResponse(); var dataStream = webResponse.GetResponseStream(); //Download the data in chuncks var dataBuffer = new byte[1024]; //Get the total size of the download var dataLength = (int)webResponse.ContentLength; //lets declare our downloaded bytes event args var byteArgs = new ByteArgs {Downloaded = 0, Total = dataLength}; //we need to test for a null as if an event is not consumed we will get an exception if (BytesDownloaded != null) BytesDownloaded(byteArgs); //Download the data var memoryStream = new MemoryStream(); while (true) { //Let's try and read the data if (dataStream != null) { int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length); if (bytesFromStream == 0) { byteArgs.Downloaded = dataLength; byteArgs.Total = dataLength; if (BytesDownloaded != null) BytesDownloaded(byteArgs); //Download complete break; } else { //Write the downloaded data memoryStream.Write(dataBuffer, 0, bytesFromStream); byteArgs.Downloaded = bytesFromStream; byteArgs.Total = dataLength; if (BytesDownloaded != null) BytesDownloaded(byteArgs); } } } //Convert the downloaded stream to a byte array byte[] downloadedData = memoryStream.ToArray(); //Release resources dataStream.Close(); memoryStream.Close(); //Write bytes to the specified file var newFile = new FileStream(targetFolder + file, FileMode.Create); newFile.Write(downloadedData, 0, downloadedData.Length); newFile.Close(); return true; } catch (Exception) { //We may not be connected to the internet //Or the URL may be incorrect return false; } }
public static bool DownloadFromWeb(string url, string file, string targetFolder) { try { //open a data stream from the supplied URL WebRequest webReq = WebRequest.Create(url + file); var webResponse = webReq.GetResponse(); var dataStream = webResponse.GetResponseStream(); //Download the data in chuncks var dataBuffer = new byte[1024]; //Get the total size of the download var dataLength = (int)webResponse.ContentLength; //lets declare our downloaded bytes event args var byteArgs = new ByteArgs { Downloaded = 0, Total = dataLength }; //we need to test for a null as if an event is not consumed we will get an exception if (BytesDownloaded != null) { BytesDownloaded(byteArgs); } //Download the data var memoryStream = new MemoryStream(); while (true) { //Let's try and read the data if (dataStream != null) { int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length); if (bytesFromStream == 0) { byteArgs.Downloaded = dataLength; byteArgs.Total = dataLength; if (BytesDownloaded != null) { BytesDownloaded(byteArgs); } //Download complete break; } else { //Write the downloaded data memoryStream.Write(dataBuffer, 0, bytesFromStream); byteArgs.Downloaded = bytesFromStream; byteArgs.Total = dataLength; if (BytesDownloaded != null) { BytesDownloaded(byteArgs); } } } } //Convert the downloaded stream to a byte array byte[] downloadedData = memoryStream.ToArray(); //Release resources dataStream.Close(); memoryStream.Close(); //Write bytes to the specified file var newFile = new FileStream(targetFolder + file, FileMode.Create); newFile.Write(downloadedData, 0, downloadedData.Length); newFile.Close(); return(true); } catch (Exception) { //We may not be connected to the internet //Or the URL may be incorrect return(false); } }