Winform--文件上传到服务器
2016-07-20 11:42
701 查看
/// <summary>
/// FTP上传文件
/// </summary>
/// <param name="userName">FTP用户名</param>
/// <param name="pwd">FTP密码</param>
/// <param name="filename">文件路径</param>
/// <param name="ftpPath">服务器路径</param>
/// <returns></returns>
public string UploadFTP(string userName, string pwd, string filename, string ftpPath)
{
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
//根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(userName, pwd);
//默认为true,连接不会被关闭
reqFTP.UsePassive = false;
//不关闭到fpt服务器的连接
reqFTP.KeepAlive = false;
//执行上传命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//读取文件流
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
return "文件上传成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 通过共享方式上传
/// </summary>
/// <param name="fileNamePath">文件路径</param>
/// <param name="urlPath">共享地址</param>
/// <param name="User">用户名</param>
/// <param name="Pwd">密码</param>
public string UpLoadFileImage(string fileNamePath, string urlPath, string User, string Pwd)
{
FileInfo fileInf = new FileInfo(fileNamePath);
string newFileName = fileInf.Name;
if (urlPath.EndsWith(@"\") == false)
{
urlPath = urlPath + @"\";
}
urlPath+="ECG" + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(urlPath))
{
Directory.CreateDirectory(urlPath);
}
urlPath = urlPath + @"\" + newFileName;
WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath);
string resultStr = "";
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
resultStr ="文件上传成功";
}
else
{
resultStr= "文件上传失败";
}
postStream.Close();
return resultStr;
}
catch
{
return "文件上传时遇到Bug";
}
}
参数:
/// FTP上传文件
/// </summary>
/// <param name="userName">FTP用户名</param>
/// <param name="pwd">FTP密码</param>
/// <param name="filename">文件路径</param>
/// <param name="ftpPath">服务器路径</param>
/// <returns></returns>
public string UploadFTP(string userName, string pwd, string filename, string ftpPath)
{
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
//根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(userName, pwd);
//默认为true,连接不会被关闭
reqFTP.UsePassive = false;
//不关闭到fpt服务器的连接
reqFTP.KeepAlive = false;
//执行上传命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//读取文件流
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
return "文件上传成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 通过共享方式上传
/// </summary>
/// <param name="fileNamePath">文件路径</param>
/// <param name="urlPath">共享地址</param>
/// <param name="User">用户名</param>
/// <param name="Pwd">密码</param>
public string UpLoadFileImage(string fileNamePath, string urlPath, string User, string Pwd)
{
FileInfo fileInf = new FileInfo(fileNamePath);
string newFileName = fileInf.Name;
if (urlPath.EndsWith(@"\") == false)
{
urlPath = urlPath + @"\";
}
urlPath+="ECG" + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(urlPath))
{
Directory.CreateDirectory(urlPath);
}
urlPath = urlPath + @"\" + newFileName;
WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath);
string resultStr = "";
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
resultStr ="文件上传成功";
}
else
{
resultStr= "文件上传失败";
}
postStream.Close();
return resultStr;
}
catch
{
return "文件上传时遇到Bug";
}
}
参数:
相关文章推荐
- RxJava 简易上手指南
- 关于对数组和指针的测试与分析OC
- 消息循环相关
- 关于APK瘦身值得分享的一些经验
- 使用JMeter3.0实战之分布式并发测试以及web API接口测试
- hdu 5723 (最小生成树 + 树上的期望)
- 聚类 - 1 - 聚类介绍
- wordpress如何屏蔽wp-json(禁用REST API)
- Json.Net组件指定/忽略序列化字段属性技巧知识点
- 把EXCEL用程序导入到ORACLE中(SpringMVC+MyBatis)
- Spring中拦截/和拦截/*的区别 - 不能访问到返回的JSP - 访问静态资源(jpg,js等)
- javax.el.ELException
- (多校第一场1001)HDU5723 Abandoned country
- Oracle学习记录整理笔记1-数据备份
- Linux环境下git客户端的安装、配置及基本用法
- Win7 64Bit + VS2012 + openBLAS0.2.18
- poj 2251 Dungeon Master 简单队列与BFS
- opengl + linux 安装体会
- Gerrit+(nginx/Apache) 的git CodeReview平台
- 如何将PDF转为可编辑的Word文档