예제 #1
0
        public string Download(string fileURL, string filePath, string msgId = "")
        {
            if (string.IsNullOrWhiteSpace(msgId))
            {
                msgId = TimeStamp.Create();
            }
            FileMessageInfo info = FileMessageInfo.Create(fileURL);

            info.MessageId   = msgId;
            info.ProcessType = FileProcessType.DOWNLOAD;
            info.FilePath    = filePath;
            info.Status      = 1;
            info.Client.DownloadProgressChanged += webClient_DownloadProgressChanged;
            info.Client.DownloadFileCompleted   += Client_DownloadFileCompleted;
            new TaskFactory().StartNew(() =>
            {
                try
                {
                    info.Client.DownloadFileAsync(new Uri(fileURL), filePath, info);
                }
                catch (Exception ex)
                {
                    ProcessError(msgId, ex);
                }
            });
            m_FileMsgQueue.Add(info);
            return(msgId);
        }
예제 #2
0
        /// <summary>
        /// 断点下载,需服务器支持CustomRange
        /// </summary>
        /// <param name="fileURL"></param>
        /// <param name="filePath"></param>
        /// <param name="msgId"></param>
        /// <returns></returns>
        private string Download_BreakPoint(string fileURL, string filePath, string msgId = "")
        {
            if (string.IsNullOrWhiteSpace(msgId))
            {
                msgId = TimeStamp.Create();
            }
            long currentLength = 0;

            if (File.Exists(filePath))
            {
                var fileInfo = new FileInfo(filePath);
                currentLength = fileInfo.Length;
            }
            FileMessageInfo info = FileMessageInfo.Create(fileURL);

            info.Client.Headers.Add("CustomRange", string.Format("bytes={0}-", currentLength));
            info.MessageId   = msgId;
            info.FilePath    = filePath;
            info.ProcessType = FileProcessType.DOWNLOAD;
            info.Status      = 1;
            m_FileMsgQueue.Add(info);
            info.Client.DownloadDataCompleted   += Client_DownloadDataCompleted;
            info.Client.DownloadProgressChanged += webClient_DownloadProgressChanged;
            new TaskFactory().StartNew(() =>
            {
                info.Client.DownloadDataAsync(new Uri(fileURL), info);
            });
            return(msgId);
        }
예제 #3
0
        public void Upload(string msg_id, string upload_url, string path)
        {
            FileMessageInfo info = FileMessageInfo.Create(upload_url);

            info.MessageId   = msg_id;
            info.ProcessType = FileProcessType.UPLOAD;
            info.Client.UploadProgressChanged += UploadProgressChanged;
            info.Client.UploadDataCompleted   += UploadDataCompleted;
            info.Status   = 1;
            info.FilePath = path;
            m_FileMsgQueue.Add(info);

            var    createBytes = new CreateBytes();
            string contentType = createBytes.ContentType;

            info.Client.Headers.Add("Content-Type", contentType);
            //new TaskFactory().StartNew(() =>
            //{

            //});

            new TaskFactory().StartNew(() =>
            {
                // LogOpInfo("upload_begin" ,Thread.CurrentThread.ManagedThreadId.ToString());
                try
                {
                    int leftLength       = (int)GetUploadStartPos(upload_url);
                    byte[] fileData      = File.ReadAllBytes(path).Skip(leftLength).ToArray();
                    info.StartPos        = leftLength;
                    ArrayList bytesArray = new ArrayList();
                    bytesArray.Add(createBytes.CreateFieldData("file", Path.GetFileName(path)
                                                               , "application/octet-stream", fileData));
                    byte[] postData      = createBytes.JoinBytes(bytesArray);
                    info.TotalFileLength = postData.Length + leftLength;
                    info.Client.UploadDataAsync(new Uri(upload_url), "POST", postData, info);

                    //LogOpInfo("upload_end", Thread.CurrentThread.ManagedThreadId.ToString());
                }
                catch (Exception ex)
                {
                    ProcessError(msg_id, ex);
                }
            });
        }