public async Task DownLoadFileAsync(FtpTransferResult ftpResult, CancellationTokenSource cancelltionTokenSource) { byte[] buffer = new byte[BUFFER_LENGTH]; FtpWebRequest request = FtpWebRequest.Create("ftp://" + this.Ftp.Host + ":" + this.Ftp.Port + ftpResult.Info) as FtpWebRequest; request.Credentials = new NetworkCredential(this.Ftp.UserName, this.Ftp.Password); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Timeout = 10000; request.ReadWriteTimeout = 10000; using (var response = await request.GetResponseAsync() as FtpWebResponse) using (var stream = response.GetResponseStream()) using (var filestream = new FileStream(ftpResult.Target, FileMode.CreateNew, FileAccess.Write, FileShare.None)) { int readed = 0; while ((readed = await stream.ReadAsync(buffer, 0, BUFFER_LENGTH)) > 0) { if (cancelltionTokenSource.IsCancellationRequested) { break; } await filestream.WriteAsync(buffer, 0, readed); ftpResult.Position += readed; ftpResult.Process = (double)ftpResult.Position / ftpResult.TotalLength * 100; } await filestream.FlushAsync(); } }
private async Task UpLoadFile(FtpFile selected) { FtpTransferResult result = new FtpTransferResult(); result.ResultType = FtpResultType.Uploading; result.Info = this._localPath + selected.Name; result.Target = this._remotePath + selected.Name; result.TotalLength = selected.ByteSize; result.Time = DateTime.Now; result.Process = 0; result.Position = 0; this._ftpResults.Add(result); this._queuedMissions.Add(result); try { await this.FtpService.UpLoadFileAsync(result); this._ftpResults.Add(new FtpTransferResult { ResultType = FtpResultType.Uploaded, Info = result.Info, Target = result.Target, Process = 100, Time = DateTime.Now }); this._remoteValue.Clear(); this._queuedMissions.Remove(result); await this.ListRemoteFiles(); } catch (Exception) { this._queuedMissions.Remove(result); throw; } }
private async Task ResumeDownLoadFile(FtpTransferResult resume) { this._ftpResults.Add(resume); this._queuedMissions.Add(resume); await this.FtpService.ResumeDownLoadFileAsync(resume, this._cancelTokanSource); this._ftpResults.Add(new FtpTransferResult { ResultType = FtpResultType.Downloaded, Info = resume.Info, Target = resume.Target, Process = 100, Time = DateTime.Now }); this._queuedMissions.Remove(resume); }
private async Task DownLoadFile(FtpFile selected) { FtpTransferResult result = new FtpTransferResult(); result.ResultType = FtpResultType.Downloading; result.Info = this._remotePath + selected.Name; result.Target = this._localPath + selected.Name; result.TotalLength = selected.ByteSize; result.Time = DateTime.Now; result.Position = 0; result.Process = (double)result.Position / result.TotalLength * 100; this._ftpResults.Add(result); this._queuedMissions.Add(result); try { await this.FtpService.DownLoadFileAsync(result, this._cancelTokanSource); this._ftpResults.Add(new FtpTransferResult { ResultType = FtpResultType.Downloaded, Info = result.Info, Target = result.Target, Process = 100, Time = DateTime.Now }); this._localValue.Clear(); this._queuedMissions.Remove(result); await this.ListLocalFiles(); } catch (Exception) { this._unCompletedMissions.Add(result); this._queuedMissions.Remove(result); File.Delete(result.Target); throw; } }
public async Task UpLoadFileAsync(FtpTransferResult ftpResult) { byte[] buffer = new byte[BUFFER_LENGTH]; FtpWebRequest request = FtpWebRequest.Create("ftp://" + this.Ftp.Host + ":" + this.Ftp.Port + ftpResult.Target) as FtpWebRequest; request.Credentials = new NetworkCredential(this.Ftp.UserName, this.Ftp.Password); request.Method = WebRequestMethods.Ftp.UploadFile; request.Timeout = 10000; request.ReadWriteTimeout = 10000; using (var stream = await request.GetRequestStreamAsync()) using (var filestream = new FileStream(ftpResult.Info, FileMode.Open, FileAccess.Read, FileShare.Read)) { int readed = 0; while ((readed = await filestream.ReadAsync(buffer, 0, BUFFER_LENGTH)) > 0) { await stream.WriteAsync(buffer, 0, readed); ftpResult.Position += readed; ftpResult.Process = (double)ftpResult.Position / ftpResult.TotalLength * 100; } await stream.FlushAsync(); } }