Пример #1
0
        private void DownloadFileCallback(object obj)
        {
            _handler          = new HttpDownloadFileHandler(_currParams.download_url, _currParams.localPath_url);
            _handler.Progress = result => _fileResult = result;
            _request          = WebRequest.Create(_currParams.download_url) as HttpWebRequest;
            _request.Timeout  = _currParams.timeout;
            _request.AddRange(_handler.DownloadedLength);
            _request.Proxy = WebRequest.DefaultWebProxy;
            try
            {
                _response = _request.GetResponse() as HttpWebResponse;
                _handler.ReceiveContentLength((int)_response.ContentLength);

                //byte[] buffer = new byte[1024*200];
                byte[] buffer = new byte[1024];
                using (Stream stream = _response.GetResponseStream())
                {
                    int dataLength = stream.Read(buffer, 0, buffer.Length);
                    while (dataLength > 0)
                    {
                        if (isStop || _handler.ReceiveData(buffer, dataLength) == false)
                        {
                            return;
                        }
                        dataLength = stream.Read(buffer, 0, buffer.Length);
                    }
                }
                _handler.Dispose();
                _EventQueue.Enqueue(new SWebDownloadEvent(DownloadEventType.OneComplete, (int)_response.StatusCode));
            }
            catch (WebException e)
            {
                //Debug.LogError(e.Message);
                int code = 0;
                _response = e.Response as HttpWebResponse;
                if (_response != null)
                {
                    code = (int)_response.StatusCode;
                }
                _handler.Dispose();
                _EventQueue.Enqueue(new SWebDownloadEvent(DownloadEventType.OneComplete, code));
            }
            finally
            {
                if (_response != null)
                {
                    _response.Close();
                }
                _request.Abort();
                _fileResult = new SDownloadFileResult();
            }
        }
Пример #2
0
 public void DownloadFile(string download_url, string localPath_url, int timeout, Action <SDownloadFileResult> progress, Action <int> complete)
 {
     _EventQueue.Clear();
     _currParams = new SWebDownloadParams();
     _currParams.download_url  = download_url;
     _currParams.localPath_url = localPath_url;
     _currParams.timeout       = timeout;
     _currParams.OnProgress    = progress;
     _currParams.OnComplete    = complete;
     _fileResult = new SDownloadFileResult();
     ThreadPool.SetMaxThreads(5, 5);
     ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadFileCallback), null);
 }
Пример #3
0
        public UnityDownloadFileHandler(string download_url, string localPath_url) : base(new byte[1024 * 200])
        {
            _download_url       = download_url;
            _localPath_url      = localPath_url;
            _downloadFileResult = new SDownloadFileResult();
            string dir = Path.GetDirectoryName(localPath_url);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            _fileStream      = new FileStream(localPath_url + ".tmp", FileMode.Append, FileAccess.Write);
            DownloadedLength = (int)_fileStream.Length;
            ContentLength    = 0;
            _downloadFileResult.downloadedLength    = DownloadedLength;
            _downloadFileResult.downloadedLengthStr = Util.HumanReadableFilesize(DownloadedLength);
        }
Пример #4
0
 private void OnProgress(SDownloadFileResult result)
 {
     Progress(new SDownloadEventResult(DownloadEventType.Progress, result));
 }