protected void AddToDownloadQueue(FileDownloadData file) { FileDownloadCount++; lock (_filesToDownload) { _filesToDownload.Add(file); } }
public static HttpResponseMessage AsFileResult(this FileDownloadData downloadData) { var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(downloadData.FileStream) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue(downloadData.ContentType); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = downloadData.FileName }; return(result); }
private void DownloadFilesThread() { while (true) { FileDownloadData fileDownloadData = null; lock (_filesToDownload) { if (_filesToDownload.Count > 0) { fileDownloadData = _filesToDownload[0]; _filesToDownload.RemoveAt(0); } } if ((fileDownloadData == null) && (FinishedReadingTree.WaitOne(0, false))) { return; } if (fileDownloadData != null) { bool retry = true; while (retry) { if (WaitingForStop.WaitOne(0, false)) { return; } try { DownloadFile(fileDownloadData); retry = false; } catch (Exception ex) { Messaging.WriteToScreen("Failed to download: " + ex.Message); } } } else { Thread.Sleep(100); } } }
public static void DownloadFile(FileDownloadData file) { Messaging.WriteToScreen("Downloading File: " + file.Uri); var directory = new DirectoryInfo(Path.GetDirectoryName(file.Path)); if (!directory.Exists) { directory.Create(); } WebRequest webRequest = WebRequest.Create(file.Uri); WebResponse webResponse = null; Stream responseStream = null; try { webResponse = webRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); using (var fs = new FileStream(file.Path, FileMode.Create)) { var buffer = new byte[1024]; int readSize; while ((readSize = responseStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, readSize); } } } finally { if (responseStream != null) { responseStream.Close(); } if (webResponse != null) { webResponse.Close(); } } }
public void PrepareFileToDownload(int requestId, FileDownloadData fileDownloadData) { _requestIdToFileStream[requestId] = fileDownloadData; }