Click here to Skip to main content
15,795,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I need to upload multiple files to an ftp server, i tried to send each file a time using Webrequest, the problem is that each time I send a file i need to add credentials , which means i open up a new session. I tried different approach but it didn't work, below my latest. Does anyone have an idea how to do it gracefully? I need to send a file a time, so i can't use asynchronous. can anyone help me please ?

private void envoiFTP(string table)
              string path = @"D:\Temp\";
                string[] files = Directory.GetFiles(path,@"*.xml");        
                if (files != null)
                    foreach (string file in files)
                        fi = new FileInfo(file);
                        string fileName = fi.Name;
                        string fileurl = path + @"/" + fileName;
                        string ftpFile = FtpServer + @"/" + fileName;
                        FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);

                        myRequest.Credentials = new NetworkCredential(FtpUser, FtpPassword);

                        myRequest.Method = WebRequestMethods.Ftp.UploadFile;
                        myRequest.Timeout = 1000000;
                        myRequest.UseBinary = true;
                        myRequest.KeepAlive = true;
                        myRequest.ContentLength = fi.Length;

                        byte[] buffer = new byte[4097];
                        int bytes = 0;
                        int total_bytes = (int)fi.Length;
                        System.IO.FileStream fs = fi.OpenRead();
                        System.IO.Stream rs = myRequest.GetRequestStream();

                         while (total_bytes > 0)
                             bytes = fs.Read(buffer, 0, buffer.Length);
                             rs.Write(buffer, 0, bytes);
                             total_bytes = total_bytes - bytes;


                         FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
Updated 28-May-13 7:50am
koriewhite 16-Jun-16 17:39pm    
Hello Suvabrata Roy..
please can you help me with the solution you provided for Salouak.i'm having the same problem. thanks


If you want to upload file asynchronously : Link[^]
Some More info[^]

Share this answer
SalouaK 28-May-13 7:56am    
No i can't uploaded asynchronously , I need to send only one file at time
Suvabrata Roy 28-May-13 8:07am    
Above code will help you to upload file to a FTP site.

But I think some where we are having some communication gap.
SalouaK 28-May-13 8:09am    
yes i think so to, i said i can not use asynchronous function , i need to only send a file a time and when the upload is finished go to the next file ....
Suvabrata Roy 28-May-13 8:18am    
Ok I understand.

write a function which will upload file to FTP site with one in parameter FilePath and one out parameter error
Create a thread
list up you files or create a static list where you can add files at run time
when a file upload completes you remove that file from Queue.
so if your queue is empty you have nothing to upload..

You need more help feel free to ask.
SalouaK 28-May-13 11:17am    
I did write a function , i will edit my question so u will be able to see it, but i stii have the same issue , i have to connecte for everyWebrequest, is there a way to send all the files but without connect and deconnect ?
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)";

if (fileobj.ShowDialog() == DialogResult.OK)
if (fileobj.CheckFileExists)
string test = Properties.Settings.Default.Connection;
SqlConnection con = new SqlConnection(test);
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);

//For Progressbar
DataTable dt = new DataTable();

timer5.Enabled = true;

string path = Application.StartupPath.Substring(0, Application.StartupPath.Length - 10);

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

string uploadUrl = "";
FileStream fs = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);

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

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

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("", "test@123");
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);

requestObj.Timeout = 1000000;
requestObj = null;

//requestObj.ConnectionGroupName = "MyGroupName";
//requestObj.KeepAlive = false;

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);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900