ASP.net操作FTP(下)

來源:互聯網
上載者:User

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[i-1].ToString():arrs[i-1].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[10240];
                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("<script>alert('file does not exist!');</script>");
            }
            ftp.Close();
        }
    }
}

這個dll在擷取ftp檔案清單方便存在些問題,等我修改下會放出一個改進版本,所以暫時用資料庫儲存ftp檔案清單先將就下:

資料庫:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FTP]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FTP]
GO

CREATE TABLE [dbo].[FTP] (
    [AutoID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [FileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [FilePath] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [FileDownloadName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [FileSize] [int] NULL ,
    [UploadTime] [datetime] NULL 
) ON [PRIMARY]
GO

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.