Net FTP 上传下载 进度条
11
/// <summary> /// 文件上传 /// </summary> /// <param name="filePath">原路径(绝对路径)包括文件名</param> /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param> public static bool FileUpLoad(string filePath, string objPath = "") { bool isOk = false; string url = path; if (objPath != "") url += objPath + "/"; FtpWebRequest reqFTP = null; ProgressBarForm progressBarForm = null; try { FileInfo fileInfo = new FileInfo(filePath); reqFTP = getFtpWebRequest(url, fileInfo.Name); progressBarForm = new ProgressBarForm(); progressBarForm.Show(); using (FileStream fs = fileInfo.OpenRead()) using (Stream stream = reqFTP.GetRequestStream()) { int buff = 1024 * 1024; byte[] b = new byte[buff]; int count = 1; double total = Math.Ceiling(fs.Length / (float)buff); int len; while ((len = fs.Read(b, 0, b.Length)) > 0) { stream.Write(b, 0, len); progressBarForm.LoadProgressBarRate(count++, (float)total); } } isOk = true; } catch (Exception ex) { Logger.Error(ex); } finally { if (progressBarForm != null) progressBarForm.Close(); } return isOk; } private static FtpWebRequest getFtpWebRequest(string url, string fileInfoName) { string requestUri = url + fileInfoName; FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(requestUri)); reqFTP.Credentials = new NetworkCredential(username, password); //设置连接到FTP的帐号密码 reqFTP.KeepAlive = false; //设置请求完成后是否保持连接 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; //指定执行命令 reqFTP.UseBinary = true; //指定数据传输类型 return reqFTP; } public void Download(string remoteFile, string localFile) { ProgressBarForm progressBarForm = null; try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(path + "/" + remoteFile); ftpRequest.Credentials = new NetworkCredential(username, password); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); ftpStream = ftpResponse.GetResponseStream(); long serverFileSize = GetFileSize(remoteFile); FileStream localFileStream = new FileStream(localFile, FileMode.Create); byte[] byteBuffer = new byte[bufferSize]; progressBarForm = new ProgressBarForm(); progressBarForm.Show(); int len; while ((len = ftpStream.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { localFileStream.Write(byteBuffer, 0, len); var current = (localFileStream.Length * 1.0d / serverFileSize) * 100; progressBarForm.LoadProgressBarRate((int)current, 100); } localFileStream.Close(); ftpStream.Close(); ftpResponse.Close(); ftpRequest = null; } catch (Exception ex) { Logger.Error(ex); Console.WriteLine(ex.ToString()); } finally { if (progressBarForm != null) progressBarForm.Close(); } }
分类:
Net
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了