黑妞与白胖
愿得一人心,白首不相离!

 

 1             string imgPath = string.Empty;//图片路径
 2             string ftpFileName = System.Configuration.ConfigurationManager.AppSettings["QuickLink"];//在配置文件中定义存图片的文件夹
 3             HttpFileCollectionBase httpFiles = Request.Files;
 4 
 5             #region 上传图片相关处理
 6             HttpPostedFileBase imgPathFile = httpFiles["fileImgPath"];  //获取界面传来的图片路径           
 7             //如果界面传来的文件名不为空,则把图片上传到FTP,并返回FTP路径 
 8             if (!string.IsNullOrEmpty(imgPathFile.FileName))
 9             {
10                 imgPath = FtpUpImage(ftpFileName, link.ID.ToString(), imgPathFile);//把图片上传至FTP
11                 link.ImgPath = imgPath;
12             }
13             #endregion
 1        #region FTP文件上传
 2         /// <summary>
 3         /// 快速链接图片上传
 4         /// </summary>
 5         /// <param name="path">文件路径</param>
 6         /// <param name="fileName">文件夹名</param>
 7         /// <returns></returns>
 8         public string FtpUpImage(string ftpFileName, string linkId, HttpPostedFileBase uploadImage)
 9         {
10             string imgPath = string.Empty;   //图片路径
11             string fileName = string.Empty;  //文件名           
12             string ftpPath = string.Empty;   //FTP路径
13 
14             fileName = uploadImage.FileName;//上传的图片名称
15 
16             int len = fileName.Split('.').Length;
17             string extName = fileName.Split('.')[len - 1].ToString();//文件后缀名                 
18             string fName = linkId + "." + extName;//文件名
19             //判断本地是否存在这个文件夹
20             if (!Directory.Exists(Server.MapPath("../UploadFile/LinkImage")))
21             {
22                 Directory.CreateDirectory(Server.MapPath("../UploadFile/LinkImage"));
23             }
24             imgPath = Server.MapPath("../UploadFile/LinkImage/") + fName;//本地路径
25             uploadImage.SaveAs(imgPath); //保存到项目组中                   
26 
27             #region 上传至FTP
28             //判断文件夹是否存在,并上传至FTP
29             if (CommonHelper.FtpCreateFolder(ftpFileName))
30             {
31                 string FTPServiceIP = ConfigurationManager.AppSettings["FTPServiceIP"]; //FTP服务器地址
32                 string folderPath = string.Format("ftp://{0}/{1}", FTPServiceIP, ftpFileName); //FTP的文件夹路径
33                 string fPath = string.Format("ftp://{0}/{1}/{2}", FTPServiceIP, ftpFileName, fName); //FTP的文件路径
34 
35                 //判断文件是否已存在,如果已存在则先删除文件再上传
37                 if (!saveList.CheckDirectory(folderPath, fName))
38                 {
39                     ftpPath = CommonHelper.FtpUploadCommon(imgPath, ftpFileName);//获取FTP的图片路径  
40                 }
41                 else
42                 {
43                     saveList.DeleteFile(fPath);//删除FTP已存在的文件
44                     ftpPath = CommonHelper.FtpUploadCommon(imgPath, ftpFileName);//获取FTP的图片路径  
45                 }
46             }
47             #endregion
51 
52             //删除本地存储的图片
53             FileInfo file = new FileInfo(imgPath);
54             if (file.Exists)
55             {
56                 file.Delete(); //删除单个文件   
57             }
59         
60             return ftpPath;
61         }
62         #endregion

//FTP上传公用方法
 1   /// <summary>
 2         /// FTP上传公用方法
 3         /// </summary>
 4         /// <param name="filename">源文件物理地址</param>
 5         /// <param name="ftpFolderName">Ftp服务器上目录名称</param>
 6         public static string FtpUploadCommon(string filename, string ftpFolderName)
 7         {
 8             string ftpServerIP = ConfigurationManager.AppSettings["FTPServiceIP"];
 9             string ftpUserID = ConfigurationManager.AppSettings["FTPUserName"];
10             string ftpPassword = ConfigurationManager.AppSettings["FTPPwd"];
11             string excelFolder = ftpFolderName;
12             string downLoadIp = ConfigurationManager.AppSettings["FTPView"];
13             if (!downLoadIp.Substring(10).Contains("/"))
14             {
15                 downLoadIp = downLoadIp + "/";
16             }
17 
18             FileInfo fileInf = new FileInfo(filename);
19 
20             string downloadUrl = (downLoadIp.Contains("http://") ? string.Empty : "http://") + downLoadIp + excelFolder + "/" + fileInf.Name;
21             FtpWebRequest reqFTP;
22 
23             // 根据uri创建FtpWebRequest对象 
24             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + excelFolder + "/" + fileInf.Name));
25 
26             // ftp用户名和密码
27             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
28 
29             // 默认为true,连接不会被关闭
30             // 在一个命令之后被执行
31             reqFTP.KeepAlive = false;
32 
33             // 指定执行什么命令
34             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
35 
36             // 指定数据传输类型
37             reqFTP.UseBinary = true;
38 
39             // 上传文件时通知服务器文件的大小
40             reqFTP.ContentLength = fileInf.Length;
41 
42             // 缓冲大小设置为2kb
43             int buffLength = 2048;
44 
45             byte[] buff = new byte[buffLength];
46             int contentLen;
47 
48             // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
49             FileStream fs = fileInf.OpenRead();
50             try
51             {
52                 // 把上传的文件写入流
53                 Stream strm = reqFTP.GetRequestStream();
54 
55                 // 每次读文件流的2kb
56                 contentLen = fs.Read(buff, 0, buffLength);
57 
58                 // 流内容没有结束
59                 while (contentLen != 0)
60                 {
61                     // 把内容从file stream 写入 upload stream
62                     strm.Write(buff, 0, contentLen);
63 
64                     contentLen = fs.Read(buff, 0, buffLength);
65                 }
66 
67                 // 关闭两个流
68                 strm.Close();
69                 fs.Close();
70                 return downloadUrl;
71             }
72             catch (Exception ex)
73             {
74                 HttpContext.Current.Response.Write("Upload Error:" + ex.Message);
75                 return string.Empty;
76             }
77         }
FtpUploadCommon
 // FTP删除操作
 1   #region FTP删除操作
 2         /// <summary>
 3         /// FTP删除操作
 4         /// </summary>
 5         /// <param name="ftpPath">路径</param>
 6         /// <param name="userId">用户名</param>
 7         /// <param name="pwd">密码</param>
 8         /// <param name="fileName">文件名</param>
 9         /// <returns></returns>
10         public string DeleteFile(string ftpPath)
11         {
12             string sRet = "删除成功!";
13             FtpWebResponse Respose = null;
14             FtpWebRequest reqFTP = null;
15             Stream localfile = null;
16             Stream stream = null;
17             try
18             {
19                 //根据uri创建FtpWebRequest对象  
20                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
21 
22                 //提供账号密码的验证  
23                 reqFTP.Credentials = new NetworkCredential(FTPUserName, FTPPwd);
24 
25                 //默认为true是上传完后不会关闭FTP连接  
26                 reqFTP.KeepAlive = false;
27 
28                 //执行删除操作
29                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
30 
31                 Respose = (FtpWebResponse)reqFTP.GetResponse();
32             }
33             catch (Exception ex)
34             {
35                 sRet = ex.Message;
36                 LogHelper.Error("Error", "TOpOfferSaveListBusiness.DeleteFile", ex);
37             }
38 
39             finally
40             {
41                 //关闭连接跟流
42                 if (Respose != null)
43                     Respose.Close();
44                 if (localfile != null)
45                     localfile.Close();
46                 if (stream != null)
47                     stream.Close();
48             }
49 
50             //返回执行状态
51             return sRet;
52         }
53  #endregion
DeleteFile

 


 

 
posted on 2013-08-31 10:49  慢阳阳  阅读(1169)  评论(0编辑  收藏  举报