【发布时间】:2020-06-01 00:12:50
【问题描述】:
我正在处理 ASP.NET 项目,我需要一个用户从 FTP 服务器下载文件并保存到他/她的本地计算机的功能。这些文件位于不同的 FTP 服务器中,并且 ASP.NET 项目托管在不同的服务器中。所以要下载,我传递了服务器地址和 FTP 凭据。当我在本地主机上运行项目时它可以工作,但是当我将项目上传到服务器并尝试从托管站点下载时,文件不会下载并保存到我的电脑上。
这是我下面的代码
string inputfilepath = @"C:\Temp\"+_filename;
string ftphost = "advhost11@14.182.126.8:3131";
string ftpfilepath = _filename;
string ftpfullpath = "ftp://" + ftphost +"/"+ ftpfilepath;
using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential("username", "password");
byte[] fileData = request.DownloadData(ftpfullpath);
Directory.CreateDirectory(Path.GetDirectoryName(inputfilepath));
using (FileStream file = File.Create(inputfilepath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Receipt downloaded and saved at C:\\\\Temp\\\\BankTransfer.');", true);
}
【问题讨论】: