FTP File Upload

Source: Internet
Author: User
Tags ftp file

Uploading files is a common function. A module for uploading images was created some time ago. I started using the shared folder method, and later found that this method is not very good. Therefore, the system was decisively killed and later used FTP for upload. I personally feel that the FTP method is still quite useful, so I 'd like to share with you.

 


Core uploaded code:


[Csharp]
/// <Summary>
/// Upload files over FTP
/// </Summary>
/// <Param name = "fileUpload"> upload Control </param>
/// <Param name = "ftpServerIP"> Upload File Server IP address </param>
/// <Param name = "ftpUserID"> Server user name </param>
/// <Param name = "ftpPassword"> server password </param>
/// <Returns> </returns>
Public string Upload (FileUpload fileUpload, string ftpServerIP, string ftpUserID, string ftpPassword)
{
String filename = fileUpload. FileName;
String sRet = "Upload successful! ";
FileInfo fileInf = new FileInfo (fileUpload. PostedFile. FileName );
String uri = "ftp: //" + ftpServerIP + "/" + filename;
FtpWebRequest reqFTP;
 
// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (uri ));
 
// Ftp user name and password
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
 
// The default value is true, and the connection will not be closed
// Executed after a command
ReqFTP. KeepAlive = false;
 
// Specify the command to be executed
ReqFTP. Method = WebRequestMethods. Ftp. UploadFile;
 
// Specify the data transmission type
ReqFTP. UseBinary = true;
ReqFTP. UsePassive = false;
 
// Notify the server of the file size when uploading the file
ReqFTP. ContentLength = fileInf. Length;
 
// Set the buffer size to 2 kb.
Int buffLength = 2048;
 
Byte [] buff = new byte [buffLength];
Int contentLen;
 
// Open a file stream (System. IO. FileStream) to read the uploaded file
FileStream fs = fileInf. OpenRead ();
 
Try
{
// Write the uploaded file to the stream
Stream strm = reqFTP. GetRequestStream ();
 
// 2kb of each file stream read
ContentLen = fs. Read (buff, 0, buffLength );
 
// The stream content has not ended
While (contentLen! = 0)
{
// Write content from file stream to upload stream
Strm. Write (buff, 0, contentLen );
 
ContentLen = fs. Read (buff, 0, buffLength );
}
 
// Close two streams
Strm. Close ();
Fs. Close ();
}
Catch (Exception ex)
{
SRet = ex. Message;
}
Return sRet;
}

/// <Summary>
/// Upload files over FTP
/// </Summary>
/// <Param name = "fileUpload"> upload Control </param>
/// <Param name = "ftpServerIP"> Upload File Server IP address </param>
/// <Param name = "ftpUserID"> Server user name </param>
/// <Param name = "ftpPassword"> server password </param>
/// <Returns> </returns>
Public string Upload (FileUpload fileUpload, string ftpServerIP, string ftpUserID, string ftpPassword)
{
String filename = fileUpload. FileName;
String sRet = "Upload successful! ";
FileInfo fileInf = new FileInfo (fileUpload. PostedFile. FileName );
String uri = "ftp: //" + ftpServerIP + "/" + filename;
FtpWebRequest reqFTP;

// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (uri ));

// Ftp user name and password
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );

// The default value is true, and the connection will not be closed
// Executed after a command
ReqFTP. KeepAlive = false;

// Specify the command to be executed
ReqFTP. Method = WebRequestMethods. Ftp. UploadFile;

// Specify the data transmission type
ReqFTP. UseBinary = true;
ReqFTP. UsePassive = false;

// Notify the server of the file size when uploading the file
ReqFTP. ContentLength = fileInf. Length;

// Set the buffer size to 2 kb.
Int buffLength = 2048;

Byte [] buff = new byte [buffLength];
Int contentLen;

// Open a file stream (System. IO. FileStream) to read the uploaded file
FileStream fs = fileInf. OpenRead ();

Try
{
// Write the uploaded file to the stream
Stream strm = reqFTP. GetRequestStream ();

// 2kb of each file stream read
ContentLen = fs. Read (buff, 0, buffLength );

// The stream content has not ended
While (contentLen! = 0)
{
// Write content from file stream to upload stream
Strm. Write (buff, 0, contentLen );

ContentLen = fs. Read (buff, 0, buffLength );
}

// Close two streams
Strm. Close ();
Fs. Close ();
}
Catch (Exception ex)
{
SRet = ex. Message;
}
Return sRet;
}
The above is just a simple upload. Of course, you still need to verify the file before uploading, such as the file format or file size. For simple verification, see determine the file format and size before uploading an image.

To prevent duplicate names, you can use GUID to generate a random sequence. Ideally, no computer or computer cluster generates two identical guids. Of course, the probability of repetition is not 0, but it is very small.


[Csharp]
/// <Summary>
/// Generate a Globally Unique Identifier
/// </Summary>
/// <Returns> </returns>
Public string strGUID ()
{
String strguid = Guid. NewGuid (). ToString ();
Return strguid;
}

/// <Summary>
/// Generate a Globally Unique Identifier
/// </Summary>
/// <Returns> </returns>
Public string strGUID ()
{
String strguid = Guid. NewGuid (). ToString ();
Return strguid;
}


Call this method and splice the returned sequence with the file name to effectively avoid file duplication. Of course, you can also splice the file name with the current system time, which may make you feel safer. The specific method is wise.

 


 

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.