Exemple #1
0
        public static void DownloadAndConvertAsync(DownloadAndConvertParams downloadAndConvertParams)
        {
            var param = new DownloadParams();

            document = null;

            param.BaseURL  = downloadAndConvertParams.DownloadUri.ToString();
            param.FileName = "stockQuotes_%mm%dd%yyyy";
            param.FromDate = downloadAndConvertParams.FromDate;
            param.ToDate   = downloadAndConvertParams.ToDate;

            DownloadAndConvertHelper._outputSettings = downloadAndConvertParams.OutputSettings;
            ConvertCompleteCallback  = downloadAndConvertParams.ConvertCompleteCallback;
            BeforeConvertCallback    = downloadAndConvertParams.BeforeConvertCallback;
            ProcessCompletedCallback = downloadAndConvertParams.ProcessCompletedCallback;
            ConvertErrorHandler      = downloadAndConvertParams.ConvertErrorHandler;

            var downloader = new ReportDownloader(param, downloadAndConvertParams.SavePath, ProcessDownloadedFile);

            downloader.OnReportDownloadCompletedEvent += downloadAndConvertParams.ReportDownloadedHandler;
            downloader.OnDownloadAllCompletedEvent    += downloadAndConvertParams.DownloadAllCompletedHandler;
            downloader.OnDownloadAllCompletedEvent    += (s, e) => {
                downloadAndConvertParams.ProcessCompletedCallback(document);
            };

            downloader.OnStartDownloadProcess  += downloadAndConvertParams.OnStartDownloadProcess;
            downloader.OnDownloadProgressEvent += downloadAndConvertParams.DownloadProgressHandler;
            downloader.Download();
        }
Exemple #2
0
        private static void ProcessDownloadedFile(object sender, AsyncCompletedEventArgs e)
        {
            ReportDownloader downloader = sender as ReportDownloader;

            if (e.Error != null)
            {
                if (downloader.CurrentDownloadFile != null)
                {
                    File.Delete(downloader.CurrentDownloadFile);
                }
            }
            else
            {
                // convert the downloaded files
                //foreach (var downloadedFile in downloader.DownloadedFiles)
                //for (int i = 0; i < downloader.DownloadedFiles.Count; i++)
                //{
                if (downloader.DownloadedFiles.Count == 0)
                {
                    return;
                }

                var downloadedFile = (from x in downloader.DownloadedFiles
                                      where x.Filename == downloader.CurrentDownloadFile
                                      select x).First();       //DownloadedFiles[i];

                try
                {
                    if (downloadedFile == null)
                    {
                        throw new Exception("Unable to find file to convert.");
                    }

                    BeforeConvertCallback(downloadedFile.Filename);
                    document = Helpers.LoadFromReportFile(downloadedFile.Filename, DownloadAndConvertHelper._outputSettings);
                    downloadedFile.Converted = true;
                    ConvertCompleteCallback(downloader, document);
                }
                catch (Exception ex)
                {
                    downloadedFile.Converted = false;
                    ConvertErrorHandler(downloader, ex);
                }
                //}
            }
        }