zoukankan      html  css  js  c++  java
  • 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[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

  • 相关阅读:
    接口分类
    HTTPS和HTTP的主要区别
    协议类
    小程序
    Polyfill
    去重数组
    odoo 接口跨域请求报错
    docker-compose 自定义容器名称
    linux ssh 防暴力破解fail2ban
    odoo 知识点
  • 原文地址:https://www.cnblogs.com/scgw/p/1944610.html
Copyright © 2011-2022 走看看