mono touch下载文件到本地

来源:互联网 发布:网络上的静静图片 编辑:程序博客网 时间:2024/05/22 06:05

有不明白的地方欢迎入群 347636249 探讨


System.Threading.Tasks.Task Task1 = System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.Sleep(500); 
ExpertDownloadBind (this, TBView, tableItems, UserID, loadingimglist.ToArray ());
});
Task1.Wait ();

System.Threading.Thread.Sleep(1000); 
TBView.Source = new TableSource (this, tableItems, UserID);
TBView.ReloadData ();


/// <summary>
/// 绑定数据并下载文件
/// </summary>       

 private void ExpertDownloadBind(UIViewController viewcontroller , UITableView tableview, 

List<TableItem> listmodel, string userid, params string[] imgarr)
{
string _fullname = string.Empty; //存储文件名        
        for (int i = 0; i < imgarr.Length; i++) {
                _fullname = imgarr [i].Substring (imgarr [i].LastIndexOf ("/") + 1);
                string _cp = FileHelper.GetMyDocumentPath (KeyCenter.Key_LoadedImgPath);//获取图片文件夹路径

                if (FileHelper.FileUploaded (_cp, _fullname))
                    continue; //已经下载过了就不下载

                string tempimgurl = imgarr [i];
                //LoadFile (tempimgurl);
                new System.Threading.Thread (() => LoadFile (tempimgurl)).Start ();
            }
        }


void LoadFile(object fileurl)
        {
            string name = fileurl.ToString ();
            var webClient = new WebClient();
            webClient.DownloadDataCompleted += (s, e) => {
                var bytes = e.Result; // get the downloaded data
                string documentsPath = FileHelper.GetMyDocumentPath (KeyCenter.Key_LoadedImgPath);
                string localFilename = name.Substring (name.LastIndexOf ("/") + 1);
                string localPath = Path.Combine (documentsPath, localFilename);

                File.WriteAllBytes (localPath, bytes); // writes to local storage   
            };
            webClient.DownloadDataAsync(new Uri(name));
        }


0 0
原创粉丝点击