To Upload a file on FTP design a windows form like below:
We can upload and download a file on FTP using System.Net. FtpWebRequest and System.Net. FtpWebResponse class,
To upload we can call this property:
request.Method = WebRequestMethods.Ftp.UploadFile;
To Download we can call this property:
request.Method = WebRequestMethods.Ftp.DownloadFile;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace UploadDownloadFTP
{
public partial class frmUpload : Form
{
public frmUpload()
{
InitializeComponent();
}
private void frmUpload_Load(object sender, EventArgs e)
{
}
private void btnBrows_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = openFileDialog1.FileName;
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("anonymous", "naveenranjan@gmail.com");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(txtFilePath.Text);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
lblUploadStatus.Text= "Upload File Complete status: " + response.StatusDescription;
response.Close();
}
}
}
To Download a File from FTP, Design a windows form as given below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace UploadDownloadFTP
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnBrows_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("anonymous", "naveenranjan@gmail.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
lblDownloadStatus.Text = "Download Complete, status "+ response.StatusDescription;
reader.Close();
response.Close();
}
}
}
}
No comments:
Post a Comment