[Solved] Upload Multiple Files to a FTp server within one session – CodeProject

Please Sign up or sign in to vote.

hi try this below code for upload file in FTP Server

private void Upload144P_Click(object sender, EventArgs e)
{
OpenFileDialog fileobj = new OpenFileDialog();
fileobj.Filter = “Movie files (*.mp4)|*.mp4|Web Movie files (*.webm)|*.webm|All files (*.*)|*.*”;
//fileobj.InitialDirectory = “C:\\”;
//fileobj.Filter = “Video files (*.mp4)”;
//fileobj.ShowDialog();

if (fileobj.ShowDialog() == DialogResult.OK)
{
if (fileobj.CheckFileExists)
{
string test = Properties.Settings.Default.Connection;
SqlConnection con = new SqlConnection(test);
con.Open();
string correctfilename = System.IO.Path.GetFileName(fileobj.FileName);
SqlCommand cmd = new SqlCommand(“Insert into Path(ID,Path5) VALUES ((select isnull(MAX(id),0) + 1 from Path),’\\Videos\\” + correctfilename + “‘)”, con);

cmd.ExecuteNonQuery();
//For Progressbar
DataTable dt = new DataTable();

timer5.Enabled = true;

string path = Application.StartupPath.Substring(0, Application.StartupPath.Length – 10);
con.Close();

string uploadfile = fileobj.FileName;
string uploadFileName = new FileInfo(uploadfile).Name;

string uploadUrl = “ftp://ftp.infotech.com/”;
FileStream fs = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);

try
{
long FileSize = new FileInfo(uploadfile).Length; // File size of file being uploaded.
Byte[] buffer = new Byte[FileSize];

fs.Read(buffer, 0, buffer.Length);

fs.Close();
fs = null;
string ftpUrl = string.Format(“{0}/{1}”, uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential(“[email protected]”, “test@123”);
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);

requestObj.Timeout = 1000000;
requestStream.Flush();
requestObj = null;

//requestObj.ConnectionGroupName = “MyGroupName”;
//requestObj.KeepAlive = false;
//requestObj.ServicePoint.CloseConnectionGroup(“MyGroupName”);

requestObj.UsePassive = true;
requestObj.UseBinary = true;
requestObj.KeepAlive = false;
}
catch (Exception ex)
{
//MessageBox.Show(“File upload/transfer Failed.\r\nError Message:\r\n” + ex.Message, “Succeeded”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

Permalink

Share this answer

Posted 16-Feb-16 21:56pm

Balaram224

Xổ số miền Bắc