

check if we reached file size to avoid unnecessary download (110% problem) while (existLen 0)ĭim sSourceURL As String = " " ' source Dim sDestinationPath As String = " " ' file destination Dim iFileSize As Long = 0 Dim iBufferSize As Integer = 1024 Var fileMode = existLen > 0 ? FileMode.Append : FileMode.Create // refactored using ( var saveFileStream = new FileStream(destinationPath, fileMode, FileAccess.Write, FileShare.ReadWrite)) Using (Stream smRespStream = hwRes.GetResponseStream()) // use using to dispose of resources HttpWebResponse hwRes = (HttpWebResponse)hwRq.GetResponse() HttpWebRequest hwRq = ()(DownloadFile.Url) If (existLen >= DownloadFile.Size) // file size is stored the first time only to check if download is necessary Var destinationPath = Path.Combine(DownloadFile.Directory, DownloadFile.Name) įileInfo fINfo = new FileInfo(destinationPath) SaveFileStream.Write(downBuffer, 0, iByteSize) While ((iByteSize = smRespStream.Read(downBuffer, 0, downBuffer.Length)) > 0) SmRespStream = hwRes.GetResponseStream() īyte downBuffer = new byte System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileMode.Append, System.IO.FileAccess.Write, SaveFileStream = new System.IO.FileStream(sDestinationPath, New System.IO.FileInfo(sDestinationPath) If (System.IO.File.Exists(sDestinationPath)) Static void DownloadFile( string sSourceURL, string sDestinationPath) The most important point is that in partial downloading, the length is required to add to our HttpWebRequest by the method HttpWebRequest.AddRange(length) which downloads content after the existing length… For this, we use the FileInfo class from which we check the length of the downloaded content. After that we are required to know how much content has been downloaded already.


If already downloaded, then we create an object of the FileStream class with Append mode otherwise we create an object of FileScream with Create mode. Before we create a request for the file we want to download, we should know if the file which we are going to download has already been downloaded previously or if this is the first request. The below code uses the FileStream, HttpWebRequest, and HttpWebResponse classes and their methods. This process uses the HttpWebRequest and HttpWebResponse classes of. Here we will learn how to download a file partially so that the we can download a big file easily in parts. But due to network interruptions, downloading a file is a problem when the size of the file is large. As we all know, downloading a file from a website is done by the click on a link, but if we want to develop an application which downloads a list of files for us, we use a web request to download files.
