about ftp download method

Last Reply 2 days ago By dharmendr

Posted 12 days ago

hi

I am using ftp download method on asp.net mvc project , its working if file less than 50 MB otherwise return like timeout , so the download method not respond long time , I think I need to change download method , its like below

            var request = (FtpWebRequest)WebRequest.Create(adres + Filename);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            //request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Credentials = new NetworkCredential(FtpUser, FtpPwd);
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            var response = (FtpWebResponse)request.GetResponse();
            using (var stream = new MemoryStream())
            {
                response.GetResponseStream().CopyTo(stream);
                Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(adres + Filename));
                //Response.ContentType = "application/zip";
                Response.ContentType = "application/force-download";
                Response.AddHeader("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(stream.ToArray());
            }
            response.Close();
            Response.Close();

 

You are viewing reply posted by: alya14 3 days ago.
Posted 3 days ago

I am trying two different method like below

        public FileStreamResult DownloadFile(string Filename)
        {      
            var request = (FtpWebRequest)WebRequest.Create(adres + Filename);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(FtpUser, FtpPwd);
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            var response = (FtpWebResponse)request.GetResponse();
            var stream = new MemoryStream();
            
                response.GetResponseStream().CopyTo(stream);
                Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(adres + Filename));
                //Response.ContentType = "application/zip";
                Response.ContentType = "application/force-download";
                Response.AddHeader("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(stream.ToArray());            
            response.Close(); 

            
          return File(stream, Response.ContentType, Filename);            
        }
		
	 public FileStreamResult DownloadFile(string Filename)
        {  
	    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            byte[] buffer = new byte[32 * 1024];
            FileStream file = System.IO.File.Create(@"d:\test\" + Filename + "");
            int read;
            //reader.Read(
            while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                file.Write(buffer, 0, read);
            }            
            responseStream.Close();
            response.Close();
			
	    return File(file, Response.ContentType, Filename);            
        }