Winform--文件上传到服务器
来源:互联网 发布:淘宝网做包包用的漩扣 编辑:程序博客网 时间:2024/06/06 05:52
/// <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";
}
}
参数:
0 0
- Winform--文件上传到服务器
- WinForm 上传文件到服务器上
- winform上传文件至服务器
- C# Winform文件上传服务器
- winform 上传文件到ftp
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- 上传文件到服务器
- C# Winform 上传本地文件到服务器路径
- C# Winform 上传本地文件到服务器路径
- Oracle学习记录整理笔记1-数据备份
- 关于对数组和指针的测试与分析OC
- hdu 5723 (最小生成树 + 树上的期望)
- 使用JMeter3.0实战之分布式并发测试以及web API接口测试
- OPENCV 3.0.0 win10 vs2013 配置
- Winform--文件上传到服务器
- 制定你的创业计划
- ubuntu安装systemtap
- 聚类 - 2 - K-means算法,K中值聚类
- ANDROID应用性能调优的技术点
- VisualStudio2013如何完全卸载
- Eclipse Java代码折叠插件 Code Folding
- linux下gdb的安装与调试
- JavaScript教程&笔记