0
我可以使asp应用程序或mvc应用程序可以使用ftp从其他服务器(站点)下载数据。我有2个网站,我想使用ftp从一个站点下载文件到另一个站点。从网络服务器下载数据的Asp代码?
我可以使asp应用程序或mvc应用程序可以使用ftp从其他服务器(站点)下载数据。我有2个网站,我想使用ftp从一个站点下载文件到另一个站点。从网络服务器下载数据的Asp代码?
是的。
从How to Download Files with FTP复制的示例。这个例子是一个控制台应用程序,但我认为你可以很容易地计算出如何将它适配到你的网站的结构。
你可能会考虑的一件事就是在网站上托管文件(如普通的HTTP),然后通过WebClient而不是FTP下载。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
}
}