上传文件。普通和ftp
/// 上传文件
/// </summary>
/// <param name="path">文件保存位置</param>
/// <param name="oFile">文件</param>
/// <returns></returns>
public string UpLoadFile( System.Web.UI.WebControls.FileUpload fu)
{
bool Isftp = true;
if (Isftp)
{
#region Ftp
try
{
string filename = fu.PostedFile.FileName;
if (fu.PostedFile.InputStream.Length > 5242880)
{
return "";
}
string DHost = "FTPHost";
string DPort = "FTPPort";
string DLogin = "FTPLogin";
string Dpwd = "FTPpwd";
string Host = "";
string Login = "";
string Password = "";
string Port = "";
Host = ConfigurationManager.ConnectionStrings[DHost].ConnectionString;
Login = ConfigurationManager.ConnectionStrings[DLogin].ConnectionString;
Password = ConfigurationManager.ConnectionStrings[Dpwd].ConnectionString;
Port = ConfigurationManager.ConnectionStrings[DPort].ConnectionString;
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
Guid gg = Guid.NewGuid();
string fileName = gg.ToString() + "_" + filename.Substring(filename.LastIndexOf("\\") + 1);
string path = "ftp://" + Host + "//" + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fu.PostedFile.InputStream.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
Stream fs = (Stream)fu.PostedFile.InputStream;
// 把上传的文件写入流
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 fileName;
}
catch (Exception ex)
{
return ex.Message;
}
#endregion
}
else{
string rStr = "";
//首先得到上载文件信息和文件流
//System.Web.HttpFileCollection oFile = System.Web.HttpContext.Current.Request.Files;
string filePath = fu.FileName; ;
Guid gg = Guid.NewGuid();
string fileName = gg.ToString()+"_"+ filePath.Substring(filePath.LastIndexOf("\\") + 1);
try
{
///
byte[] b = new byte[fu.PostedFile.InputStream.Length];
Stream fs;
//InLoadFiles oLoad = new InLoadFiles();
//UpLoad u = new UpLoad();
fs = (Stream)fu.PostedFile.InputStream;
fs.Read(b, 0,(int)fu.PostedFile.InputStream.Length);
//取得服务器库物理地址
string libPath = ConfigurationManager.AppSettings["LibPath"];
if (string.IsNullOrEmpty(libPath))
throw new Exception("服务器库物理地址配置信息找不到");
string path = string.Format(@"{0}\{1}", libPath, fileName);
rStr = UpLoadStream(b, fileName, path);
}
catch (Exception error)
{
rStr = error.Message;
//Response.Write(error.Message);
//Response.End();
}
return rStr;
}
}
/// <summary>
/// 上传文件流
/// </summary>
/// <param name="fs">流文件</param>
/// <param name="fileName">文件名称</param>
/// <param name="requestPath">保存路径</param>
/// <returns></returns>
protected string UpLoadStream(byte[] fs, string fileName, string requestPath)
{
try
{
MemoryStream m = new MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
FileStream f = new FileStream(requestPath, FileMode.Create);
///把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return fileName;
}
catch (Exception error)
{
return error.Message;
}
}