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 = * ;
byte[] b = new byte[buff];
int count = ;
double total = Math.Ceiling(fs.Length / (float)buff);
int len;
while ((len = fs.Read(b, , b.Length)) > )
{
stream.Write(b, , 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, , byteBuffer.Length)) > )
{
localFileStream.Write(byteBuffer, , len);
var current = (localFileStream.Length * 1.0d / serverFileSize) * ;
progressBarForm.LoadProgressBarRate((int)current, );
}
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
Logger.Error(ex);
Console.WriteLine(ex.ToString());
}
finally
{
if (progressBarForm != null)
progressBarForm.Close();
}
}
FTP 上传下载 进度条的更多相关文章
- jq 上传下载进度条
里面只演示了下载的,挂载的是我的七牛服务器上的内容,上传事件和下载是一模一样的,为了大家不乱上传东西到我的服务器,而且我的服务器容量也不大,这里只展示了下载.代码: <!DOCTYPE html ...
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- asp.net大文件上传与上传文件进度条问题
利用Plupload解决大容量文件上传问题, 带进度条和背景遮罩层 关于Plupload结合上传插件jquery.plupload.queue的使用 这是群里面一位朋友给的资料. 下面是自己搜索到的一 ...
- windows系统下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
- node实现http上传文件进度条 -我们到底能走多远系列(37)
我们到底能走多远系列(37) 扯淡: 又到了一年一度的跳槽季,相信你一定准备好了,每每跳槽,总有好多的路让你选,我们的未来也正是这一个个选择机会组合起来的结果,所以尽可能的找出自己想要的是什么再做决定 ...
- Ajax上传文件进度条显示
要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小 html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大 小/总大小,计算上传的百分比,然后用这个百分比控制div框的显 ...
- windows下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
- FTP上传下载工具(FlashFXP) v5.5.0 中文版
软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...
随机推荐
- MyBatis知识点整理
1.MyBatis一般使用步骤 1.1获取Configuration实例或编写配置文件 //获取Configuration实例的样例 TransactionFactory transactionFac ...
- zlt项目实践
nacos gateWay fronted oath2 codeGenerate log-app monitor-app search-app
- STL顺序容器用法自我总结
顺序容器类型:vector.deque.list.forward_list.string.array. queue(单向队列)底层也是用deque(双端队列)实现的 a.swap(b); swap(a ...
- 【代码总结】Spring MVC数据校验
1.实验介绍 --------------------------------------------------------------------------------------------- ...
- 网页链接在QQ内因多人投诉被拦截的解决方案
背景 相信大家经常会遇到一个头疼的问题就是,明明自己的网页没有违规内容(比如线下活动的推广),但链接在QQ内转发分享会被QQ管家拦截,导致用户无法访问. 那么当大家遇到这个问题的时候应该怎么办呢?不用 ...
- Swiper 移动端全屏轮播图效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 吴裕雄 python 机器学习——半监督学习LabelSpreading模型
import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...
- Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...
- 修改eclipse工程jdk版本
在eclipse中项目jdk版本不匹配的时候需要修改项目工程的jdk版本,但是网上的一些版本修改不是很完全,经过一些摸索之后,参考总结了我在项目中的具体配置实践 问题: 修改eclipse中的项目jd ...
- GO学习之 从hello.go开始
一.GO运行的两种方式 1.如果是对源码编译后,再执行 如:hello.go文件 hello.go文件(源文件)----go build hello.go[编译文件]--->变成可执行文件(.e ...