0

I was trying to download files from FTP server to on C://Downloads file. Actually, I designed a page that all ftp files listed in there. An user if want to download file, must click to file name. Maybe user don't want to save file downloads file. So have you any suggesstion for that :

  1. How to select destination path to download file from ftp server by user?

I'm using MVC pattern in net core. I don't sure that download file process should be in web api side or view side.

           FtpWebRequest request = (FtpWebRequest)WebRequest.Create(downloadURL);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(this.appSettings.FTPServerUserName, this.appSettings.FTPServerPassword);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            fileName = @"c:\temp\" + fileName;
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));
            FileStream file = File.Create(fileName);
            byte[] buffer = new byte[2 * 1024];
            int read;
            while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, read); }
            file.Close();
            responseStream.Close();
            response.Close();
xangkcl
  • 57
  • 1
  • 7
  • If the user clicks on a download link, the browser will let the user choose where do download the file to (if the browser is configured to ask). You do not have to code anything for that. That's all under the browser control. Is this what you are after? [Download file from FTP and how prompt user to save/open file in ASP.NET C#](https://stackoverflow.com/q/11047879/850848). – Martin Prikryl Dec 19 '20 at 20:29
  • thaks a lot @MartinPrikryl, I try to download file from ftp with your example link. I don't understand that in the source code, file download to @"c:\temp\" folder. This path is default or not? If user select another destination folder, I should pass to destination folder as a parameter this download method? – xangkcl Dec 20 '20 at 10:28
  • Don't look at the question. The code there is wrong. Otherwise the OP won't be asking about it. Look at the code in the answer. – Martin Prikryl Dec 20 '20 at 11:18
  • I added codes. What is the type of Response object ? The type is FtpWebResponse or not, because I try to add "Response.AddHeader("content-disposition", "attachment;filename=" + strFile);", this line given error. @MartinPrikryl – xangkcl Dec 20 '20 at 12:11
  • The `response` is your ASP page response. See for an example [How to use web handlers for PDF creation?](https://stackoverflow.com/q/30075075/850848). – Martin Prikryl Dec 20 '20 at 16:06

0 Answers0