public void Download(string url, string savePath, string fileName) { Loom.RunAsync(() => { asyncTask = new AsyncTask(); asyncTask.url = url; asyncTask.savePath = savePath; asyncTask.fileName = fileName; //保存路径不存在则创建 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } //创建请求 asyncTask.httpWebRequest = WebRequest.Create(url) as HttpWebRequest; //创建文件流 asyncTask.fileStream = new FileStream(savePath + "/" + fileName, FileMode.OpenOrCreate, FileAccess.Write); //获取已下载文件长度 asyncTask.fileLength = asyncTask.fileStream.Length; //断点续传核心 //Seek 将该流的当前位置设置为给定值 asyncTask.fileStream.Seek(asyncTask.fileLength, SeekOrigin.Begin); //向请求添加范围标头 asyncTask.httpWebRequest.AddRange((int)asyncTask.fileLength); asyncTask.httpWebRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), asyncTask); }); }
void OnDestroy() { Instance = null; }