C # Implementing FTP upload Code

Source: Internet
Author: User
Tags file transfer protocol

C # Implementing FTP upload Code(2010-04-06 19:26:50) reproduced
Tags: it

Using System.Net;
Using System.IO;

Call the following function
Private Ftpstatuscode Uploadfun (string fileName, String uploadurl)
{
Stream requeststream = null;
FileStream FileStream = null;
FtpWebResponse uploadresponse = null;
Try
{
FtpWebRequest uploadrequest =
(FtpWebRequest) WebRequest.Create (Uploadurl);
Uploadrequest.method = WebRequestMethods.Ftp.UploadFile;

Uploadrequest.proxy = null;
NetworkCredential NC = new NetworkCredential ();
nc. UserName = "AA";
nc. Password = "aa123";

Uploadrequest.credentials = NC; Modify Getcredential (); Error 2


Requeststream = Uploadrequest.getrequeststream ();
FileStream = File.Open (FileName, FileMode.Open);

byte[] buffer = new byte[1024];
int bytesread;
while (true)
{
Bytesread = filestream.read (buffer, 0, buffer. Length);
if (bytesread = = 0)
Break
requestStream.Write (buffer, 0, bytesread);
}
Requeststream.close ();

Uploadresponse = (ftpwebresponse) uploadrequest.getresponse ();
return uploadresponse.statuscode;

}
catch (UriFormatException ex)
{
}
catch (IOException ex)
{
}
catch (WebException ex)
{
}
Finally
{
if (uploadresponse! = null)
Uploadresponse.close ();
if (fileStream! = null)
Filestream.close ();
if (requeststream! = null)
Requeststream.close ();
}
return ftpstatuscode.undefined;
}

invocation Example
Ftpstatuscode status = Uploadfun (@ "D:\1\1.txt", "ftp://Domain name/directory/save file name");


ftpstatuscode Return Status Description:

FTPSTATUSCODE Specifies the status code returned for the File Transfer Protocol (FTP) operation.

Accountneeded indicates that a user account on the server is required.
Actionabortedlocalprocessingerror indicates that an error has occurred that prevented the completion of the request operation.
Actionabortedunknownpagetype indicates that the requested operation cannot be taken because the specified page type is unknown. The page type is described in section 3.1, 2.3, RFC 959.
ACTIONNOTTAKENFILENAMENOTALLOWED specifies that the requested operation cannot be performed on the specified file.
ACTIONNOTTAKENFILEUNAVAILABLE specifies that the requested operation cannot be performed on the specified file because the file is not available.
ACTIONNOTTAKENFILEUNAVAILABLEORBUSY specifies that the requested operation cannot be performed on the specified file because the file is not available or is in use.
Actionnottakeninsufficientspace indicates that the requested operation cannot be performed because there is not enough space on the server.
Argumentsyntaxerror indicates that one or more command arguments have a syntax error.
Badcommandsequence indicates that the order of the command sequence is incorrect.
Cantopendata indicates that the data connection cannot be opened.
Closingcontrol indicates that the server is shutting down the administrative connection.
Closingdata indicates that the server is shutting down the data connection and the requested file operation was successful.
Commandextraneous indicates that the server did not execute the command because this command is not required.
Commandnotimplemented indicates that the FTP server did not execute the command.
Commandok indicates that the command completed successfully.
Commandsyntaxerror indicates that the command has a syntax error or is not a server-aware command.
Connectionclosed indicates that the connection is closed.
Dataalreadyopen indicates that the data connection is open and the requested transmission has started.
Directorystatus indicates the directory status.
Enteringpassive indicates that the server is entering passive mode.
Fileactionaborted indicates that the requested operation cannot be performed.
Fileactionok indicates that the requested file operation completed successfully.
Filecommandpending indicates that the requested file operation requires additional information.
Filestatus indicates the file status.
Loggedinproceed indicates that the user is logged in and can send commands.
Needloginaccount indicates that the server needs to provide a login account.
Notloggedin indicates that the logon information must be sent to the server.
Openingdata indicates that the server is opening a data connection.
Pathnamecreated indicates that the requested path name has been created.
The restartmarker indicates that the response contains a restart token reply. The description text that accompanies this status contains the user data flow token and the server tag.
Sendpasswordcommand indicates that the server needs to provide a password.
Sendusercommand indicates that the server has been able to perform user logon operations.
The serverwantssecuresession instructs the server to accept the client-specified authentication mechanism, and the secure data exchange is complete.
Servicenotavailable indicates that this service is not available.
Servicetemporarilynotavailable indicates that the service is not available now; Please try your request again later.
SystemType indicates the system type name of the system name published in the Assigned Numbers (number allocation) document published in the Internet Assigned Numbers Authority (internet number distribution authority).
Undefined contains this value for completeness, and the server never returns this value.

C # Implementing FTP upload Code

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.