. Net ftp File Upload method,. netftp File Upload Method
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using System. Net;
Using System. Text;
Using System. Configuration;
Namespace DlnOffice. general. netdisk
{
Public partial class fileupload2: System. Web. UI. Page
{
// These three constants are obtained from webconfig.
Private string ftpServerIP = ConfigurationManager. AppSettings ["ftpServerIP"];
Private string ftpUserID = ConfigurationManager. AppSettings ["ftpUserID"];
Private string ftpPassword = ConfigurationManager. AppSettings ["ftpPassword"];
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void linkbutton#click (object sender, EventArgs e)
{
Up_FTP (FileUpload1 );
// Up_FTP (FileUpload2 );
Response. Write ("<script> alert ('complete'); window. location. href = 'fileupload2. aspx '</script> ");
// Else
//{
// Response. Write ("<script> alert ('file not selected'); </script> ");
// Return;
//}
}
Public void up_FTP (FileUpload fu)
{
String AppPath = (base. Request. QueryString ["parent_folder"]! = Null )? Base. Request. QueryString ["parent_folder"]. ToString (): "/Information Department/common software ";
If (string. IsNullOrEmpty (AppPath ))
{
Base. Response. Redirect ("index. aspx ");
Return;
}
// The root directory of the file upload address. The local host is set up as the FTP server through IIS.
// String FileSaveUri = @ "ftp: // 127.0.0.1 /";
String FileSaveUri = @ "ftp: //" + ftpServerIP + AppPath + "/";
// FTP user name and password, which is the user name and password of the Local Machine
String ftpUser = ftpUserID;
String ftpPassWord = ftpPassword;
Stream requestStream = null;
Stream fileStream = null;
FtpWebResponse uploadResponse = null; // create an FtpWebResponse instance uploadResponse
// Btn_Upload.
If (fu. HasFile)
{
// Get the object Length
Int FileLength = fu. PostedFile. ContentLength;
// The maximum size of the uploaded file cannot exceed 1 GB
If (FileLength <1024*1024*1024)
{
Try
{
// Format it as URI
Uri uri = new Uri (FileSaveUri + Path. GetFileName (fu. PostedFile. FileName ));
FtpWebRequest uploadRequest = (FtpWebRequest) WebRequest. Create (uri); // Create an FtpWebRequest instance uploadRequest
UploadRequest. Method = WebRequestMethods. Ftp. UploadFile; // set the FtpWebRequest attribute to upload a file.
UploadRequest. Credentials = new NetworkCredential (ftpUser, ftpPassWord); // authenticate the FTP user name and password
RequestStream = uploadRequest. GetRequestStream (); // obtain the stream used to upload FTP
Byte [] buffer = new byte [FileLength];
FileStream = fu. PostedFile. InputStream; // captures the file Stream Obtained by FileUpload as the FTP upload stream.
FileStream. Read (buffer, 0, FileLength );
RequestStream. Write (buffer, 0, FileLength); // Write the buffer to the stream.
RequestStream. Close ();
UploadResponse = (FtpWebResponse) uploadRequest. GetResponse (); // return the FTP server response. The upload is complete.
// Upload successful
}
Catch (Exception ex)
{
// Unable to upload
Response. Write ("<script> alert ('unable to upload'); </script> ");
Return;
}
Finally
{
If (uploadResponse! = Null)
UploadResponse. Close ();
If (fileStream! = Null)
FileStream. Close ();
If (requestStream! = Null)
RequestStream. Close ();
}
} // End if # FileLength #
Else
{
// The uploaded file is too large.
Response. Write ("<script> alert ('upload file is too large, the file cannot exceed 1 gb'); </script> ");
Return;
}
} // End if # FileUpload. HasFile #
Else
{
// File not selected
Return;
}
}
}
}