/// <summary> /// Downloads a file /// </summary> /// <param name="urlDownload"></param> /// <param name="downloadToDirectory"></param> /// <param name="baseFilename">Filename without extension</param> /// <param name="downloadTypeMapper"></param> /// <returns>The path to the downloaded file</returns> private string DownloadFile_inner(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper) { //Strip off an extension if its there baseFilename = FileIOHelper.GenerateWindowsSafeFilename(System.IO.Path.GetFileNameWithoutExtension(baseFilename)); var webClient = CreateLoggedInWebClient(); using (webClient) { //Choose a temp file name to download to var starterName = System.IO.Path.Combine(downloadToDirectory, baseFilename + ".tmp"); OnlineSession.StatusLog.AddStatus("Attempting file download: " + urlDownload, -10); webClient.DownloadFile(urlDownload, starterName); //Download the file //Look up the correct file extension based on the content type downloaded var contentType = webClient.ResponseHeaders["Content-Type"]; var fileExtension = downloadTypeMapper.GetFileExtension(contentType); var finishName = System.IO.Path.Combine(downloadToDirectory, baseFilename + fileExtension); //Rename the downloaded file System.IO.File.Move(starterName, finishName); return(finishName); } }
/// <summary> /// Download a file /// </summary> /// <param name="urlDownload"></param> /// <param name="downloadToDirectory"></param> /// <param name="baseFilename"></param> /// <param name="downloadTypeMapper"></param> /// <returns>The path to the downloaded file</returns> internal string DownloadFile(string urlDownload, string downloadToDirectory, string baseFilename, DownloadPayloadTypeHelper downloadTypeMapper) { //Lets keep track of how long it took var startDownload = DateTime.Now; string outputPath; try { outputPath = DownloadFile_inner(urlDownload, downloadToDirectory, baseFilename, downloadTypeMapper); } catch (Exception) { StatusLog.AddError("Download failed after " + (DateTime.Now - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload); throw; } var finishDownload = DateTime.Now; StatusLog.AddStatus("Download success duration " + (finishDownload - startDownload).TotalSeconds.ToString("#.#") + " seconds. " + urlDownload, -10); return(outputPath); }