在我的应用程序中,我有一个要求,客户端/用户需要将视频文件从 ftp 服务器下载到他们的本地机器。
通过使用下面的代码,我可以从 ftp 下载文件,但它只保存在我的应用程序服务器上,而不是本地客户端 PC。我想将文件下载到客户端电脑,它应该在下载前询问对话框。
我的代码如下,
string ResponseDescription = "";
string PureFileName = new FileInfo(filename).Name;
string DownloadedFilePath = @"E:\\aa\\" + PureFileName;
String downloadUrl = String.Format("{0}/{1}", "ftp://abcd.int/", filename);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
req.Method = WebRequestMethods.Ftp.DownloadFile;
req.Credentials = new NetworkCredential("username", "password");
req.UseBinary = true;
req.Proxy = null;
FtpWebResponse response = (FtpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
byte[] buffer = new byte[2048];
FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
int ReadCount = stream.Read(buffer, 0, buffer.Length);
while (ReadCount > 0)
{
fs.Write(buffer, 0, ReadCount);
ReadCount = stream.Read(buffer, 0, buffer.Length);
}
ResponseDescription = response.StatusDescription;
fs.Close();
stream.Close();