示例#1
0
        public static async void DownloadAsync(IDownloadable download, IDiskStorageProvider storage, string path, string fileName)
        {
            if (download == null || download.Uri == null)
            {
                throw new ArgumentNullException(nameof(download));
            }

            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            try
            {
                Stream stream = await DownloadAsync(download);

                storage.Put(path, fileName, stream);
            }
            catch (Exception e)
            {
                download.Status    = DownloadStatus.Failed;
                download.Exception = e;
                throw e;
            }
        }
示例#2
0
        public static bool TryDownload(IDownloadable download, IDiskStorageProvider storage, string path, string fileName)
        {
            if (download == null || download.Uri == null)
            {
                throw new ArgumentNullException(nameof(download));
            }

            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (string.IsNullOrEmpty(fileName))
            {
                return(false);
            }

            try
            {
                using (IWebClient client = CreateWebClient())
                {
                    storage.Put(path, fileName, client.OpenRead(download.Uri));
                }
                download.Status = DownloadStatus.Success;
            }
            catch (Exception e)
            {
                download.Status    = DownloadStatus.Failed;
                download.Exception = e;
                return(false);
            }

            return(true);
        }