mofdan 发表于 2015-5-28 10:37:57

ASP.net操作FTP(下)

  FtpClass.cs:


using System;
using System.Configuration;
using System.IO;
using FtpSupport;
using Microsoft.Win32;
using System.Web;
namespace FtpTest
{
    public class FtpClass
    {
      private string FtpIP=ConfigurationSettings.AppSettings["FtpIP"];
      private string FtpUserName=ConfigurationSettings.AppSettings["FtpUserName"];
      private string FtpPassord=ConfigurationSettings.AppSettings["FtpPassWord"];
      private FtpConnection ftp;
      private FtpConnection FtpConn()
      {
            ftp=new FtpConnection();
            ftp.Connect(this.FtpIP,this.FtpUserName,this.FtpPassord);
            return ftp;
      }
      private string GetFileExtName(string filename,bool withdot)
      {
            string [] arrs=filename.Split('.');
            int i=arrs.Length;
            return withdot?"."+arrs.ToString():arrs.ToString();
      }
      private string GetFileContentType(string filedownloadname)
      {
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey,fileextkey;
            string FileContentType;
            try
            {               
                regkey=Registry.ClassesRoot;               
                fileextkey=regkey.OpenSubKey(this.GetFileExtName(filedownloadname,false));               
                FileContentType=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                FileContentType=DEFAULT_CONTENT_TYPE;
            }      
            return FileContentType;
      }
      public string FtpUpload(HttpPostedFile file,string dir)
      {
            string FileDownloadName=DateTime.Now.ToString("yyyyMMddhhmmss")+this.GetFileExtName(file.FileName,true);
            FtpConnection ftp=this.FtpConn();
            if(ftp.DirectoryExist(dir))
                ftp.SetCurrentDirectory(dir);
            else
            {
                ftp.CreateDirectory(dir);
                ftp.SetCurrentDirectory(dir);
            }
            ftp.PutStream(file.InputStream,FileDownloadName);
            ftp.Close();
            return FileDownloadName;
      }
      public void FileDel(string filedownloadname,string dir)
      {
            FtpConnection ftp=this.FtpConn();
            if(ftp.DirectoryExist(dir))
            {
                ftp.SetCurrentDirectory(dir);
                if(ftp.FileExist(filedownloadname))
                {
                  ftp.DeleteFile(filedownloadname);
                }
            }
            ftp.Close();
      }
      public void FtpDownload(HttpContext context,string filedownloadname,string dir)
      {
            context.Response.Clear();
            context.Response.AddHeader("Content-Disposition", "attachment; filename="+filedownloadname);
            context.Response.ContentType=this.GetFileContentType(filedownloadname);
            FtpConnection ftp=this.FtpConn();
            ftp.SetCurrentDirectory(dir);
            if(ftp.FileExist(filedownloadname))
            {
                FtpStream ftpfs=ftp.OpenFile(filedownloadname,GenericRights.Read);
                byte [] buffer=new byte;
                int n=ftpfs.Read(buffer,0,buffer.Length);
                while(n>0)
                {
                  context.Response.BinaryWrite(buffer);
                  n=ftpfs.Read(buffer,0,buffer.Length);
                }
                Response.End();
                ftpfs.Close();
            }
            else
            {
                context.Response.Write("alert('file does not exist!');");
            }
            ftp.Close();
      }
    }
}
  
这个dll在获取ftp文件列表方便存在些问题,等我修改下会放出一个改进版本,所以暂时用数据库保存ftp文件列表先将就下:
数据库:


if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table .
GO
CREATE TABLE . (
    (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    (50) COLLATE Chinese_PRC_CI_AS NULL ,
    (50) COLLATE Chinese_PRC_CI_AS NULL ,
    (50) COLLATE Chinese_PRC_CI_AS NULL ,
    NULL ,
    NULL
) ON
GO
页: [1]
查看完整版本: ASP.net操作FTP(下)