Skip to content

How to donwload file from ftp in asp.net

Demir Acar

Asked Jun 6, 2015, 3:47 PM EDT4 answers
Share
LinkedInX
hi guys ;
 
I listed the ftp files on gridview Asp.net and also I wanna download listed ftp files , How Can I do .
 
I have 4 columns in gridview , Filename, File Size , Created Date , Download , When I click the download button wanna the start download process .
 
Thank in advance . 

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    0
    Hi,
     
    You can try this code :
     
    C#
     private void Download(string filePath, string fileName)
    {
        FtpWebRequest reqFTP;
        try
        {
            //filePath = <<The full path where the
            //file is to be created. the>>,
            //fileName = <<Name of the file to be createdNeed not
            //name on FTP server. name name()>>
            FileStream outputStream = new FileStream(filePath +
                                    "\\" + fileName, FileMode.Create);
    
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
                                    ftpServerIP + "/" + fileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                        ftpPassword);
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];
    
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }
    
            ftpStream.Close();
            outputStream.Close();
            response.Close();
        }
        catch (Exception ex)
        {
    
        }
    }
    For more details check this aticles :
    0
  • 0
    Hi , thank you for quick respond , the code that you shared is Works fine , But I wanna get the dialog when file saved , So users choice to save path .
    0
  • Manoj Bhoir
    0
    Hi,
     
    C#
    If you want user to select file location to save use this code :
    
    private void Download(string filePath, string fileName)
            {
                FtpWebRequest reqFTP;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
                    ftpServerIP + "/" + fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID,
                    ftpPassword);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    long cl = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];
    
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    ftpStream.Close();
                    response.Close();
                    this.Response.ClearContent();
                    this.Response.Clear();
                    this.Response.ContentType = "APPLICATION/OCTET-STREAM";
                    String Header = "Attachment; Filename=" + fileName;
                    this.Response.AppendHeader("Content-Disposition", Header);
                    this.Response.BinaryWrite(buffer);
                }
                catch (Exception ex)
                {
    
                }
                this.Response.End(); // This line is very important
            }
    0
  • 0
    Hi again ; the method that you are last shared , if downloaded file small than 10 mb working fine if file bigger than 10 MB ,the respond was crashed , Have an idea ?
    0