/// 注册下载文件 public uint RegisteFileHttpDownload(string url, string filePath, HttpFileSessionDelegate handler = null) { if (string.IsNullOrEmpty(url) || (filePath == null)) { JW.Common.Log.LogE("RegisteFileHttpDownload:Arg Error"); return(0); } if ((!url.Contains("http://")) && (!url.Contains("https://"))) { url = "http://" + url; } HttpFileSession hf = new HttpFileSession(); hf.Url = url; hf.DownloadFilePath = filePath; hf.Id = _currentId++; hf.Handler = handler; if (hf != null && (hf.IsOver == false)) { _httpSessions.Add(hf); } return(hf.Id); }
//更新获取数据 public void Update() { //等待销毁 if (IsOver) { return; } if (_isError) { Stop(); _isOver = true; } if (_isOver) { Stop(); if (_isError) { if (Handler != null) { Handler(true, Id, true, 0.0f); } Handler = null; } else { if (Handler != null) { Handler(false, Id, true, 1.0f); } Handler = null; } //等待销毁 IsOver = true; return; } //若干时间,HttpWebRequest始终没有调用回调(如果回调成功调用,_response等三个对象不会为空),就结束当前下载任务。 if (_response == null || _responseStream == null || _fileStream == null) { _waitedAsyn += 50; if (_waitedAsyn >= TimeOut * 3) { Debug.Log("等待响应超时!"); _isError = true; _isOver = true; } return; } // int readSize = 0; bool isException = false; try { readSize = _responseStream.Read(_buff, 0, BuffLen); } catch (Exception ex) { Log.LogE("HttpFileSession Read Exception :" + ex.Message); isException = true; } finally { if (isException) { _isError = true; } else { if (readSize > 0) { _fileStream.Write(_buff, 0, readSize); // _curDownloadLength += readSize; float progress = (float)_curDownloadLength / _totalFileLength; //JW.Common.Log.LogD("FileHttpSession:Downloaded:"+_curDownloadLength+":"+_totalFileLength); if (Handler != null) { Handler(false, Id, false, progress); } //下载完成 if (_curDownloadLength >= _totalFileLength) { _isOver = true; if (_fileStream != null) { _fileStream.Close(); _fileStream.Dispose(); _fileStream = null; //移动文件 JW.Res.FileUtil.CopyFile(_tempDownLoadFilePath, DownloadFilePath, true); //删除临时文件 JW.Res.FileUtil.DeleteFile(_tempDownLoadFilePath); } } } else { JW.Common.Log.LogD("FileHttpSession:Downloaded End"); ////下载完成 if (_curDownloadLength >= _totalFileLength) { _isOver = true; if (_fileStream != null) { _fileStream.Close(); _fileStream = null; //移动文件 JW.Res.FileUtil.CopyFile(_tempDownLoadFilePath, DownloadFilePath, true); //删除临时文件 JW.Res.FileUtil.DeleteFile(_tempDownLoadFilePath); } } else { _isOver = true; _isError = true; } } } } }