Downloading files Using FtpWebRequest

前端 未结 2 1744
孤独总比滥情好
孤独总比滥情好 2020-12-29 11:55

I\'m trying to download a file using FtpWebRequest.

private void DownloadFile(string userName, string password, string ftpSourceFilePath, string         


        
2条回答
  •  庸人自扰
    2020-12-29 12:26

    Just figured it out:

        private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
        {
            int bytesRead = 0;
            byte[] buffer = new byte[2048];
    
            FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
    
            Stream reader = request.GetResponse().GetResponseStream();
            FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
    
            while (true)
            {
                bytesRead = reader.Read(buffer, 0, buffer.Length);
    
                if (bytesRead == 0)
                    break;
    
                fileStream.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();       
        }
    

    Had to use a FileStream instead.

提交回复
热议问题