Hello
I am trying to make a bot which will download files and upload on server.

I have no idea how to do that (yet)

can someone guide me which methods will be used in downloading file and uploading (ftp server)

and one more thing
is there any size limitation on downloading files??

Thanks

Recommended Answers

All 6 Replies

Hello
I am trying to make a bot which will download files and upload on server.

I have no idea how to do that (yet)

can someone guide me which methods will be used in downloading file and uploading (ftp server)

and one more thing
is there any size limitation on downloading files??

Thanks

You can Upload with something like this:

public static bool upload(string Filepath, string username, string password)
        {
            
            string ftpHost = @"ftp://ftp.website.com/";
            string ftpUser = username;
            string ftpPass = password;
            string filename = System.IO.Path.GetFileName(Filepath);
            string fullpath = ftpHost + filename;
            Stream RequestStream = null;
            FtpWebRequest request = null;
            try
            {
                request = (FtpWebRequest)WebRequest.Create(fullpath);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.KeepAlive = true;
                request.UseBinary = true;
                request.Proxy = null;
                request.UsePassive = true;
                request.Credentials = new NetworkCredential(ftpUser, ftpPass);

                FileStream sourceStream = File.OpenRead(Filepath);
                byte[] buffer = new byte[sourceStream.Length];

                sourceStream.Read(buffer, 0, buffer.Length);
                sourceStream.Close();

                RequestStream = request.GetRequestStream();
                RequestStream.Write(buffer, 0, buffer.Length);
                RequestStream.Close();
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                FtpStatusCode code = response.StatusCode;
                string Description = response.StatusDescription;
                response.Close();
                if (code == FtpStatusCode.ClosingData)
                {
                    RequestStream.Close();
                    return true;
                }
                else
                {
                    RequestStream.Close();
                    return false;
                }

            }catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error uploading" + ex.Message);
                return false;
            }
        }

And you can Download something with this:

private static bool Download(string remoteDir, string file, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
    
            try
            {  
                FileStream writeStream;
                string uri = ftpServerIP + remoteDir + file;
                Uri serverUri = new Uri(uri);
                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return false;
                }       
                FtpWebRequest reqFTP;                
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);                                
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                
                reqFTP.KeepAlive = false;                
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                                
                reqFTP.UseBinary = true;
                reqFTP.Proxy = null;                 
                reqFTP.UsePassive = true;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream responseStream = response.GetResponseStream();
                writeStream = new FileStream(@"C:\Temp\" + file, FileMode.Create);
                
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);               
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }                
                writeStream.Close();
                response.Close();
                return true;
            }
            catch (WebException wEx)
            {

                System.Windows.Forms.MessageBox.Show(wEx.message);
                return false;
            }
            catch (Exception ex)
            {
               System.Windows.Forms.MessageBox.Show("Error while downloading file: " + file + ". Error details: " + ex.Message);
                return false;
            }
        }

You can change these functions to your liking. Parametrize server address, file, etc...

Make sure you read MSDN on FTP upload and Download. There are a couple of libraries that you can use as well... But in the code above, it's all .net 4.0 code.

Thanks alot Sir :)
I will try this out ;)

is there any size limit?
and can i download and upload multiple files at same time?

Can i download files from http by using same code?

You can Download as many files as you want, so long as you know what the file names are. Just read how to list directory contents here: http://msdn.microsoft.com/en-us/library/ms229716.aspx

I haven't tried to download files from HTTP using this, and it will probably not work. This is for FTP only.

Cheers.

HOw to get a file from File Upload Control and that file should be inserted in Sql server Database ? Can any one please help me ?

How to send a automatic confirmation mail to my website registered members ? When they click on that mail then only account should be activated !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.