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. deleettings ["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 ();
}
}
}
This dll has some problems in obtaining the FTP file list. When I modify it, an improved version will be released. Therefore, we will save the FTP file list in the database for the time being:
Database:
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