# Region ftp upload function
/// <Summary>
/// Ftp upload function
/// </Summary>
/// <Param name = "ftpServerIP"> </param>
/// <Param name = "filename"> </param>
/// <Param name = "ftpUserID"> </param>
/// <Param name = "ftpPassword"> </param>
Public static void Upload (string ftpServerIP, string filename, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo (filename );
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
FtpWebRequest reqFTP;
// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri ("ftp: //" + ftpServerIP + "/" + fileInf. Name ));
// 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;
// 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)
{
// MessageBox. Show (ex. Message, "Upload Error ");
HttpContext. Current. Response. Write ("Upload Error:" + ex. Message );
}
}
# Endregion