. Net ftp File Upload method,. netftp File Upload Method

Source: Internet
Author: User
Tags ftp file

. 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;
}
}
}
}

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.