Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MY code:
C#
//Clientpath = <<The full path where the file is located on client Machin 
        //Serverpath = <<The full serverpath where the file is creted on server.
        public void Upload(string Clientpath, string Serverpath)
        {

            try
            {
                FileInfo fileInf = new FileInfo(Clientpath);

                string uri = "ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name;

                FtpWebRequest reqFTP;

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.UsePassive = true;
                reqFTP.KeepAlive = true;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.Timeout = 3000;
             
                ServicePointManager.DefaultConnectionLimit = 10;

                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                FileStream fs = fileInf.OpenRead();

                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                strm.Close();
                strm.Flush();
                strm.Dispose();
                strm = null;
                fs.Close();
                reqFTP.GetRequestStream().Close();
                reqFTP.Abort();
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
                
                reqFTP = null;


            }
            catch (Exception ex)
            {

                MessageBox.Show("Error:   " + ex);

            }
        }

Please Help me i tried to resolve this issue from last 3 days,i read all blogs and comment from internet but still bed luck ,Please check this Experts and resolve my problem

When i try to upload multiple file at 1st time or 1st upload its working good.But when for second file upload process get hanged at "reqFTP.GetRequestStream();" and after some time is time out
Posted
Updated 3-Apr-15 0:32am
v3
Comments
Sascha Lefèvre 3-Apr-15 2:15am    
You should mention what the actual problem is.
bhatt_rupa 3-Apr-15 5:19am    
When i try to upload multiple file at 1st time or 1st upload its working good.But when for second file upload process get hanged at "reqFTP.GetRequestStream();" and after some time is time out
Sascha Lefèvre 3-Apr-15 5:27am    
Did you check out solution 1?
bhatt_rupa 3-Apr-15 8:08am    
hi i tried and done changes as following..
using (Stream strm = reqFTP.GetRequestStream())
{
using (FileStream fs = fileInf.OpenRead())
{
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}



fs.CopyTo(strm);
strm.Close();
strm.Flush();
strm.Dispose();

fs.Close();
}
// strm = null;
}
but still bed luck
Sascha Lefèvre 3-Apr-15 8:15am    
try this instead:

using (Stream strm = reqFTP.GetRequestStream())
{
  using (FileStream fs = fileInf.OpenRead())
  {
    fs.CopyTo(strm);
  }
}

Honestly there is a lot of noise in this snippet code.
You are calling GetRequestStream twice, also it literally seems like you are just calling every single stream method in no particular order or reason, I mean you are closing the stream and then flushing it and then disposing it and then creating new stream and instantly closing it and then aborting it.
I don't even want to guess what is the first issue here but do note that there are plenty of reasons why you not getting the right result from that code.

Nevertheless try something like this:
C#
FileInfo fileInf = new FileInfo(Clientpath);
string uri = string.Format("ftp://{0}/{1}/{2}", ftpServerIP, Serverpath, fileInf.Name);

var reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.Timeout = 3000;

ServicePointManager.DefaultConnectionLimit = 10;

using (Stream strm = reqFTP.GetRequestStream())
using (FileStream fs = fileInf.OpenRead())
    fs.CopyTo(strm);
 
Share this answer
 
Comments
bhatt_rupa 3-Apr-15 8:20am    
i change as following but still having problem

using (Stream strm = reqFTP.GetRequestStream())
{
using (FileStream fs = fileInf.OpenRead())
{
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}



fs.CopyTo(strm);
strm.Close();
strm.Flush();
strm.Dispose();

fs.Close();
}
// strm = null;
}
Mario Z 3-Apr-15 9:08am    
The issue is that you are first writing into a request stream:

while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}

After that you are copying into it ...
Also you don't need your Close, Flush, Dispose combo, you have using block.

Try just replacing everything that is in your try block with my snippet code.
bhatt_rupa 6-Apr-15 1:27am    
Hi as per yours i change code as below:
try
{
FileInfo fileInf = new FileInfo(Clientpath);
string uri = string.Format("ftp://{0}/{1}/{2}", ftpServerIP, Serverpath, fileInf.Name);

var reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.Timeout = 3000;

ServicePointManager.DefaultConnectionLimit = 10;

using (Stream strm = reqFTP.GetRequestStream())
using (FileStream fs = fileInf.OpenRead())
fs.CopyTo(strm);
}
catch (Exception ex)
{

// MessageBox.Show("Error: " + ex);

}

For 1st its working good but for second at same its getting hung
At this line
using (Stream strm = reqFTP.GetRequestStream())
and after some time FTP server showing this logs
(000008)4/6/2015 10:48:04 AM - sa (192.168.1.142)> 226 Successfully transferred "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA"
(000008)4/6/2015 10:50:04 AM - sa (192.168.1.142)> 421 Connection timed out.
(000008)4/6/2015 10:50:04 AM - sa (192.168.1.142)> disconnected.
Mario Z 7-Apr-15 4:08am    
I apologize for a bit late response, I'm usually unavailable on holidays ...
Now regarding your issue, I noticed that some developers are complaining about this issue with WebRequest and multiple calls.
The suggestion they mention is that we should use reqFTP.Abort();, try it and let me know if it works.
bhatt_rupa 10-Apr-15 4:25am    
Thanks For your response

Hi as per your suggestion i add last line as reqFTP.Abort();
but still getting hung at using (Stream strm = reqFTP.GetRequestStream())
during second upload
below logs from FTP Server for your referance:
000005)4/10/2015 13:39:30 PM - sa (192.168.1.142)> 227 Entering Passive Mode (192,168,1,189,4,5)
(000005)4/10/2015 13:39:30 PM - sa (192.168.1.142)> STOR /GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA//5_DPM729_SA_XXX_XXX_XX.doc
(000005)4/10/2015 13:39:30 PM - sa (192.168.1.142)> 150 Opening data channel for file upload to server of "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA/5_DPM729_SA_XXX_XXX_XX.doc"
(000005)4/10/2015 13:39:31 PM - sa (192.168.1.142)> 226 Successfully transferred "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA/5_DPM729_SA_XXX_XXX_XX.doc"
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> CWD /GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> 250 CWD successful. "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile" is current directory.
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> PASV
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> 227 Entering Passive Mode (192,168,1,189,4,6)
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> NLST 03-19-2015_SA
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> 150 Opening data channel for directory listing of "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA"
(000005)4/10/2015 13:39:34 PM - sa (192.168.1.142)> 226 Successfully transferred "/GMD-Data/vendor/GMD-India-UK/03-19-2015/Ster-Surgery/shivam/DocumentFile/03-19-2015_SA"
//Clientpath = <<The full path where the file is located on client Machin>>,
//Serverpath = <<The full serverpath where the file is creted on server>>,
public void Upload(string Clientpath, string Serverpath)
{
FileInfo fileInf = new FileInfo(Clientpath);

string uri = "ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name;

FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 500000;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();

Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
reqFTP.Abort();

}
 
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