Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to download a file from ftp server through httpwebrequest. Could anyone help me on this? Please provide the code for this.

Thanks,
Prasant



[orig title]
download file from ftp through httpwebrequest in c#.net1.1
Posted
Updated 22-Dec-10 1:11am
v3
Comments
Sergey Alexandrovich Kryukov 22-Dec-10 21:32pm    
I've written in your comment below: "I want it through httpwebrequest not through ftpwebreques".
At this point, you really should explain why.
I explained in my Answer that it has no sense.

If you disagree you should explain why, otherwise -- who will bother about your problem?
Sergey Alexandrovich Kryukov 22-Dec-10 21:35pm    
Another question: do you really need to develop in .NET v.1.1 as in the tag to your question? The thing is, this version barely exists. Real .NET starts with v.2.0 which is already good, stable and valid one. As to 1.1...

There are Thousands of examples are available in the Net..,

Go through the below link..,

Here[^]
 
Share this answer
 
Comments
User CP 22-Dec-10 7:22am    
I want it through httpwebrequest not through ftpwebrequest
Sergey Alexandrovich Kryukov 22-Dec-10 21:29pm    
prasant38: You should explain why you "want it through httpwebrequest not through ftpwebreques".
I explained it makes no sense. If you disagree please explain why, otherwise no one with bother to help.
Rajesh Anuhya 22-Dec-10 23:28pm    
prasant38: Are you go through that link.., don't post comment/vote, even you don't go through the link.., Very FIRST LINK in the search is gives you explanation about the ftpwebrequest.
No, do not look at Google search. Just regular Microsoft help provides more than enough help on the topic, complete with code samples.

The problem is different and simple. Why on earth you think you need HttpWebRequest?

HTTP and FTP are different protocols, and, generally speaking, are implemented on the same site by different servers (demons or whatever; different processes), and those servers do not have to share any data. This is also most typical. So, if some file is accessed via FTP, nothing says it also can be accessed via HTTP or HTTPS. (Even if this is the case, nothing tells you how to figure out its HTTP URL from FTP URL.) Different systems, different data, different clients are needed.

One option is using WebRequestMethods.Ftp.DownloadFile: http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.downloadfile.aspx[^].

Another option is using FtpWebRequest class: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx[^].
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 11:03am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:33pm    
Thank you, I'll reuse this one if needed.
--SA
downloadDownload the source code -423 KB




Introduction:

Today I put a sample program to manage your FTP
Perhaps all of you with many programs to work with FTP or have seen much of you need to have transferred the file to the server or servers to receive and manage the server.
It can create folders, manage and delete files and folders, upload and download files to your will.


Familiar with the concept for "FTP" and the launch method, click here




To view original size Click on the photos.

Software_FTP
View_FTP


Background:
The software language is written in C # 2008.
I plan to have written part of office automation and felt that's too difficult to copy and upload in a network environment are fully software compatible with Windows Server - XP and 7 are by all FTP Microsoft and all FTP installed on the data center support.



Comment code:
1 - Uploading a file:



 private void Btn_UploadFTP_Click(object sender, EventArgs e)
{
   string _tempServerPath = Txt_ServerPath.Text + <br />           "\\" + Path.GetFileName(Txt_LocalPath_Upload.Text);
   ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
   ftPclient1.Upload(Txt_LocalPath_Upload.Text, _tempServerPath);
}
public bool Upload(FileInfo fi, [Optional, DefaultParameterValue("")] string targetFilename)
{
  string str;
  bool _Lvr=true;
  if (targetFilename.Trim() == "")
     {
       str = this.CurrentDirectory + fi.Name;
     }
  else if (targetFilename.Contains("/"))
     {
       str = this.AdjustDir(targetFilename);
     }
   else
      {
        str = this.CurrentDirectory + targetFilename;
      }
   string uRI = this.Hostname + str;
   FtpWebRequest request = this.GetRequest(uRI);
   request.Method = "STOR";
   request.UseBinary = true;
   request.ContentLength = fi.Length;
   byte[] array = new byte[0x800];
   using (FileStream stream = fi.OpenRead())
      {
       try
         {
          using (Stream stream2 = request.GetRequestStream())
            {
             int num;
               do
                {
                  num = stream.Read(array, 0, 0x800);
                  stream2.Write(array, 0, num);
                  Application.DoEvents();<br />                  CountProcesses = <br />                             Convert.ToInt32(<br />                             Math.Round((decimal)(stream.Position * 100) / stream.Length));
                  ChangePerocesses_Upload();
                 }
                while (num >= 0x800);
               stream2.Close();
              }
           }
           catch (Exception exception1)
            {
             _Lvr = false;
              ProjectData.SetProjectError(exception1);
              Exception exception = exception1;
              ProjectData.ClearProjectError();
             }
            finally
             {
               stream.Close();
             }
        }
   request = null;
   UploadComplite();
   return _Lvr;
}


2 - Download the file:

private void button4_Click(object sender, EventArgs e)
{
   string _Source_DownloadPath = Txt_ServerPath.Text + "\\" + Txt_FileNameDownload.Text;
   ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
   ftPclient1.Download(_Source_DownloadPath, Txt_LocalPath_Download.Text, false);
}





3 - Display files:

private void button5_Click(object sender, EventArgs e)
{
    Txt_View_FtpFile.Text = string.Empty; ;
    ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
    List<string> _listFile = ftPclient1.ListDirectory("//"+Txt_ServerPath.Text);
    for (int i = 0; i < _listFile.Count; i++)
     {
       Txt_View_FtpFile.Text += _listFile[i].ToString() + Environment.NewLine;
     }
}


 
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