/// <summary>
        /// 异步下载文件
        /// </summary>
        /// <param name="serverPath"></param>
        private void DownloadFileAsync(string serverPath)
        {
            if (serverPath.Length <= 0 || string.IsNullOrEmpty(serverPath))
            {
                return;
            }
            if (webcDownloading == null)
            {
                return;
            }

            try
            {
                //将下载地址转换为Uri类
                System.Uri serverUri = new System.Uri(serverPath);
                //下载后保存的文件位置及名称
                string filePath = string.Empty;
                filePath = string.Format("{0}\\{1}", this.DownSavePath, this.DownSaveFile);
                //WebClient异步下载文件
                AsyncOperation asyncOp;
                //暂时没做暂停,继续下载功能,支持断点续传
                webcDownloading.DownloadFileAsync(serverUri, filePath);
            }
            // 'down(serverUri.OriginalString, 0, filePath)

            catch (Exception ex)
            {
                //myMsgbox(string.Format("下载文件时出错,出错信息:{0}", ex.Message), "提示信息", MessageBoxButtons.OK, SystemIcons.Error);
            }
        }
示例#2
0
        /// <summary>抓取远程头像</summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public virtual Boolean FetchAvatar(IManageUser user)
        {
            var av = user.GetValue("Avatar") as String;

            if (av.IsNullOrEmpty())
            {
                throw new Exception("用户头像不存在 " + user);
            }

            var url = av;

            if (!url.StartsWithIgnoreCase("http"))
            {
                return(false);
            }

            // 不要扩展名
            var set = Setting.Current;

            av = set.AvatarPath.CombinePath(user.ID + "").GetFullPath();

            // 头像是否已存在
            if (File.Exists(av))
            {
                return(false);
            }

            av.EnsureDirectory(true);

            try
            {
                var wc = new WebClientX(true, true);
                Task.Factory.StartNew(() => wc.DownloadFileAsync(url, av)).Wait(5000);

                //// 更新头像
                //user.SetValue("Avatar", "/Sso/Avatar/" + user.ID);

                return(true);
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }

            return(false);
        }