Asp.net FTP (lower)

Source: Internet
Author: User
Tags ftp file

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.