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 .
Manoj BhoirRank #599 · 4K pointsPosted Jun 6, 2015, 7:58 PM EDT
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 :Demir Acar73 pointsPosted Jun 7, 2015, 4:35 AM EDT
Manoj BhoirRank #599 · 4K pointsPosted Jun 7, 2015, 6:47 AM EDT
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 }Demir Acar73 pointsPosted Jun 7, 2015, 11:47 AM EDT