/// <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";
}
}
参数: