Exemplo n.º 1
0
        /// <summary>
        /// 下载一个ImageModel并写入到本地(异步)
        /// </summary>
        /// <param name="imageModel"></param>
        /// <returns>Task</returns>
        private static async Task downloadAImageAndSave(ImageModel imageModel)
        {
            try
            {
                if (imageModel.ImageUrl != null)
                {
                    Byte[] imageBytes = await HttpDownloader.DownloadBytes(imageModel.ImageUrl);

                    FileWriter.WriteToFile(imageBytes, imageModel.ImageSavePath);
                }
            }
            catch (TaskCanceledException e)
            {
                Debug.WriteLine("失败:" + "下载二进制文件超时。" + imageModel.ImageUrl);
                throw e;
            }
            catch (Exception e)
            {
                Debug.WriteLine("失败:" + imageModel.ImageUrl + " 下载失败,原因:" + e.Message);
                throw e;
            }
        }
        private static async Task ParseAndDownload(EbookPageModel webpage)
        {
            EbookPageModel page = await parser.FindEbookUrl(webpage);

            //下载图书文件
            int success = 0;

            foreach (EbookFileModel ebook in page.EBooks)
            {
                try
                {
                    byte[] file = await HttpDownloader.DownloadBytes(ebook.DownloadUrl);

                    FileWriter.WriteToFile(file, ebook.FileSavePath);
                    success++;
                    ebook.IsDownloaded = true;
                    DownloadFileCount++;
                    Debug.WriteLine("下载文件数量=" + DownloadFileCount);
                }
                catch (TaskCanceledException)
                {
                    Debug.WriteLine("下载图书文件超时");
                    continue;
                }
                catch (HttpRequestException e)
                {
                    Debug.WriteLine("访问图书下载连接失败:" + e.Message);
                    continue;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    continue;
                }
            }
            if (success == 0)
            {
                return;
            }
            //下载图书图片
            try
            {
                byte[] imageBytes = await HttpDownloader.DownloadBytes(page.Image.ImageUrl);

                FileWriter.WriteToFile(imageBytes, page.Image.ImageSavePath);
                //如果图片下载成功,则填入地址,失败则为null
                page.ImagePath = page.Image.ImageSavePath;
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("下载图片超时");
            }
            catch (HttpRequestException e)
            {
                Debug.WriteLine("访问图片连接失败:" + e.Message);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            //添加对象属性
            page.FilePaths  = string.Join(", ", page.EBooks.Where(x => x.IsDownloaded).Select(x => x.FileSavePath));
            page.FileFormat = string.Join(", ", page.EBooks.Where(x => x.IsDownloaded).Select(x => x.FileExtention));
            page.FileCount  = page.EBooks.Where(x => x.IsDownloaded).Count();
            //写入数据库
            //using (var db=new MyAppDbContext())
            //{
            //    db.Blogs.Add(page);
            //    db.SaveChanges();
            //    RecordCount++;
            //    Debug.WriteLine("写入记录条数=" + RecordCount);
            //}
        }