Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
When i use this code click on asp.Button then all operation correct but file not download. Please help me


What I have tried:

FtpWebRequest request1 = null;
            request1 = (FtpWebRequest)WebRequest.Create(ftpServerIP + Session["appid"] + "/" + fname);

            //Enter FTP Server credentials.
            request1.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            request1.UsePassive = true;
            request1.UseBinary = true;
            request1.EnableSsl = false;
            request1.Method = WebRequestMethods.Ftp.DownloadFile;
            //Fetch the Response and read it into a MemoryStream object.
            FtpWebResponse response1 = (FtpWebResponse)request1.GetResponse();
            using (MemoryStream stream = new MemoryStream())
            {
                //Download the File.
                response1.GetResponseStream().CopyTo(stream);                
                Response.AddHeader("content-disposition", "attachment;filename=" + fname);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(stream.ToArray());
                Response.End();
            }
Posted
Updated 26-Mar-23 20:16pm
v2
Comments
F-ES Sitecore 30-Aug-19 8:12am    
We can't run your code in your context so we don't know. Start by debugging the code so you can provide a more detailed question. Is request1 what you expect, is it a valid url with the write appid? Is response1 what you would expect? Does the CopyTo method copy the file into the stream as you'd expect? Is "stream.ToArray()" returning a binary file like you expect? Have you even googled how to write a file using BinaryWrite? Does the code that works match your code? Do you actually generate any response? Have you used a tool like fiddler to see the contents of that response? Is it as you expect?
Suvendu Shekhar Giri 31-Aug-19 0:39am    
Try to narrow down your investigation, see if there is an exception and share that specific case instead of a broad scenario which difficult for us to pinpoint the issue.

1 solution

private void Download(string file)
{
string FromLocalFile = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FromLocalFile"].ToString());
string ToServerFile = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ToServerFile"]).ToString();
string ftpserverIP = System.Configuration.ConfigurationManager.AppSettings["ftpserverIP"];
string username = System.Configuration.ConfigurationManager.AppSettings["username"];
string password = System.Configuration.ConfigurationManager.AppSettings["password"];
string portno = System.Configuration.ConfigurationManager.AppSettings["portno"];
string LocalDir = System.Configuration.ConfigurationManager.AppSettings["LocalDir"].ToString();
try
{
string uri = ftpserverIP + ToServerFile + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
//Ftp web request created
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpserverIP + ToServerFile + "/" + file);
request.Method = WebRequestMethods.Ftp.DownloadFile;
// FTP login credentials
request.Credentials = new NetworkCredential(username, password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Program.CreateLocalDir();//calling the createLocalDir() method
string date = DateTime.Now.ToString("ddd MM.dd.yyyy");
string foldername = string.Format(file);
FileStream writeStream = new FileStream(LocalDir + @"\" + Path.Combine(date, foldername), 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();
}
catch (WebException wEx)
{
Console.WriteLine(wEx.Message, "Download Error");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, "Download Error");
}
}
 
Share this answer
 
Comments
Dave Kreskowiak 27-Mar-23 7:59am    
An unexplained code dump is not an appropriate 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