public static DownloadData Create(string url, string destFolder, ICredentials creds) { DownloadData downloadData = new DownloadData(); WebRequest req = GetRequest(url, creds); try { if (req is FtpWebRequest) { // get the file size for FTP files req.Method = WebRequestMethods.Ftp.GetFileSize; downloadData.response = req.GetResponse(); downloadData.GetFileSize(); // send REST cmd to 0 in case after last file server doesnt cleared it already //req = GetRequest(url); //downloadData.response = req.GetResponse(); // new request for downloading the FTP file req = GetRequest(url, creds); ((FtpWebRequest)req).ContentOffset = 0; downloadData.response = req.GetResponse(); } else { downloadData.response = req.GetResponse(); downloadData.GetFileSize(); } } catch (WebException e) { throw e; } catch (Exception e) { throw new Exception(string.Format("Error downloading \"{0}\": {1}", url, e.Message), e); } // Check to make sure the response isn't an error. If it is this method // will throw exceptions. ValidateResponse(downloadData.response, url); // Take the name of the file given to use from the web server. string fileName = downloadData.response.Headers["Content-Disposition"]; if (fileName != null) { int fileLoc = fileName.IndexOf("filename=", StringComparison.OrdinalIgnoreCase); if (fileLoc != -1) { // go past "filename=" fileLoc += 9; if (fileName.Length > fileLoc) { // trim off an ending semicolon if it exists int end = fileName.IndexOf(';', fileLoc); if (end == -1) { end = fileName.Length - fileLoc; } else { end -= fileLoc; } fileName = fileName.Substring(fileLoc, end).Trim(); } else { fileName = null; } } else { fileName = null; } } if (string.IsNullOrEmpty(fileName)) { // brute force the filename from the url fileName = Path.GetFileName(downloadData.response.ResponseUri.LocalPath); } // trim out non-standard filename characters if (!string.IsNullOrEmpty(fileName) && fileName.IndexOfAny(invalidFilenameChars.ToArray()) != -1) { //make a new string builder (with at least one bad character) StringBuilder newText = new StringBuilder(fileName.Length - 1); //remove the bad characters for (int i = 0; i < fileName.Length; i++) { if (invalidFilenameChars.IndexOf(fileName[i]) == -1) { newText.Append(fileName[i]); } } fileName = newText.ToString().Trim(); } // if filename *still* is null or empty, then generate some random temp filename if (string.IsNullOrEmpty(fileName)) { fileName = Path.GetFileName(Path.GetTempFileName()); } string downloadTo = Path.Combine(destFolder, fileName); downloadData.Filename = downloadTo; // If we don't know how big the file is supposed to be, // we can't resume, so delete what we already have if something is on disk already. if (!downloadData.IsProgressKnown && File.Exists(downloadTo)) { File.Delete(downloadTo); } if (downloadData.IsProgressKnown && File.Exists(downloadTo)) { // Try and start where the file on disk left off downloadData.start = new FileInfo(downloadTo).Length; // If we have a file that's bigger than what is online, then something // strange happened. Delete it and start again. if (downloadData.start > downloadData.size) { File.Delete(downloadTo); } else if (downloadData.start < downloadData.size) { // Try and resume by creating a new request with a new start position downloadData.response.Close(); if (downloadData.response is HttpWebResponse) { req = GetRequest(url, creds); ((HttpWebRequest)req).AddRange((int)downloadData.start); downloadData.response = req.GetResponse(); if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent) { // They didn't support our resume request. File.Delete(downloadTo); downloadData.start = 0; } } else { req = GetRequest(url, creds); ((FtpWebRequest)req).ContentOffset = (int)downloadData.start; downloadData.response = req.GetResponse(); if ((downloadData.Response as FtpWebResponse).StatusCode != FtpStatusCode.FileCommandPending) { File.Delete(downloadTo); downloadData.start = 0; } } } } return(downloadData); }
// Begin downloading the file at the specified url, and save it to the given folder. private void BeginDownload() { DownloadData data = null; FileStream fs = null; try { //start the stopwatch for speed calc sw.Start(); if (!Directory.Exists(_tempDirectory)) { Directory.CreateDirectory(_tempDirectory); } data = DownloadData.Create(url_, _tempDirectory, _credentials); //reset the adler downloadedAdler32.Reset(); DownloadingTo = data.Filename; if (!File.Exists(DownloadingTo)) { // create the file fs = File.Open(DownloadingTo, FileMode.Create, FileAccess.Write); } else { // read in the existing data to calculate the adler32 if (Adler32 != 0) { GetAdler32(DownloadingTo); } // apend to an existing file (resume) fs = File.Open(DownloadingTo, FileMode.Append, FileAccess.Write); } // create the download buffer byte[] buffer = new byte[BufferSize]; int readCount; // update how many bytes have already been read sentSinceLastCalc = data.StartPoint; //for BPS calculation // Only increment once for each % int LastProgress = 0; while ((readCount = data.DownloadStream.Read(buffer, 0, BufferSize)) > 0) { // update total bytes read data.StartPoint += readCount; // update the adler32 value if (Adler32 != 0) { downloadedAdler32.Update(buffer, 0, readCount); } // save block to end of file fs.Write(buffer, 0, readCount); //calculate download speed calculateBps(data.StartPoint, data.TotalDownloadSize); // send progress info if (data.PercentDone > LastProgress) { _onProgress?.Invoke(new DownloadProgressInfo { Percentage = data.PercentDone, DownloadedInBytes = data.TotalDownloadSize, }); LastProgress = data.PercentDone; } } } catch (UriFormatException e) { throw new Exception(string.Format("Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.", url_), e); } catch (WebException e) { throw e; } catch (Exception e) { if (string.IsNullOrEmpty(DownloadingTo)) { throw new Exception(string.Format("Error trying to save file: {0}", e.Message), e); } else { throw new Exception(string.Format("Error trying to save file \"{0}\": {1}", DownloadingTo, e.Message), e); } } finally { if (data != null) { data.Close(); } if (fs != null) { fs.Close(); } } }