Using System;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.IO;
Using System.Net;
Using System.Text;
public partial class _default:system.web.ui.page
{
The following fields are configured in the Web. config
private string Ftpserverip = "127.0.0.1";//server IP
private string Ftpuserid = "ftptest";//user name Ftptest
private string FTPPassword = "ftptest";//Password
protected void Page_Load (object sender, EventArgs e)
{
if (Myfile.value! = "")
{
String a = MyFile.;
}
}
FTP upload function
private void Upload (string filename)
{
FileInfo Fileinf = new FileInfo (filename);
String uri = "ftp://" + Ftpserverip + "/" + fileinf.name;
FtpWebRequest reqftp;
To create a FtpWebRequest object from a URI
Reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/" + Fileinf.name));
FTP User name and password
Reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword);
The default is true and the connection is not closed
is executed after a command
Reqftp.keepalive = false;
Specify what commands to execute
Reqftp.method = WebRequestMethods.Ftp.UploadFile;
Specifying data Transfer Types
Reqftp.usebinary = true;
Notifies server file size when uploading a file
Reqftp.contentlength = Fileinf.length;
Buffer size set to 2KB
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 per read file stream
Contentlen = fs. Read (Buff, 0, bufflength);
Stream content does not end
while (Contentlen! = 0)
{
Write content from file stream to upload stream
Strm. Write (Buff, 0, Contentlen);
Contentlen = fs. Read (Buff, 0, bufflength);
}
//Turn off two streams
STRM. Close ();
FS. Close ();
this. Page.registerstartupscript ("", "<script>alert (' success ') </script>");
}
catch (Exception ex)
{
//MessageBox.Show (ex. Message, "Upload Error");
Response.Write ("Upload Error:" + ex.) Message);
}
}
The ability to download files from an FTP server
private void Download (String filePath, String fileName)
{
FtpWebRequest reqftp;
Try
{
FileStream outputstream = new FileStream (filePath + "\ \" + FileName, FileMode.Create);
Reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/" + fileName);
Reqftp.method = WebRequestMethods.Ftp.DownloadFile;
Reqftp.usebinary = true;
Reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword);
FtpWebResponse response = (ftpwebresponse) reqftp.getresponse ();
Stream FtpStream = Response. GetResponseStream ();
Long cl = Response. ContentLength;
int buffersize = 2048;
int readcount;
byte[] buffer = new Byte[buffersize];
Readcount = ftpstream.read (buffer, 0, buffersize);
while (Readcount > 0)
{
Outputstream.write (buffer, 0, readcount);
Readcount = ftpstream.read (buffer, 0, buffersize);
}
Ftpstream.close ();
Outputstream.close ();
Response. Close ();
}
catch (Exception ex)
{
Response.Write ("Download Error:" + ex.) Message);
}
}
Get a list of files from the FTP server
Public string[] Getfilelist ()
{
String[] Downloadfiles;
StringBuilder result = new StringBuilder ();
FtpWebRequest reqftp;
HttpWebRequest reqftp;
Try
{
Reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/"));
Reqftp.usebinary = true;
Reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword);
Reqftp.method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = Reqftp.getresponse ();
StreamReader reader = new StreamReader (response. GetResponseStream ());
String line = reader. ReadLine ();
while (line! = null)
{
Result. Append (line);
Result. Append ("\ n");
line = reader. ReadLine ();
}
To remove the trailing ' \ n '
Result. Remove (result. ToString (). LastIndexOf (' \ n '), 1);
Reader. Close ();
Response. Close ();
return result. ToString (). Split (' \ n ');
}
catch (Exception ex)
{
Downloadfiles = null;
return downloadfiles;
}
}
protected void Button1_Click (object sender, EventArgs e)
{
Upload ("F:\\ Captain America DVD in Word. rmvb");
}
protected void button2_click (object sender, EventArgs e)
{
}
}
20160113006 ASP. NET implementation FTP upload code (solve large file upload problem)