关于从FTP下载文件的总结

时间:2021-03-22 09:56:18

从网上找了很多方法,很多方法和方式,(有的直接下载有的通过浏览器打开下载(推荐后者))。但是多半都带点问题。最后从官网找到了方法。话不多说贴代码

ftpServerIP = "在这填入你的ip地址";//服务器ip 
ftpUserID = "gzytest";//用户名 
ftpPassword = "1234";//密码

string fileName = "markLine.docx";
string ftpRemotePath = "";
string ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
WebClient request = new WebClient();
request.Encoding = System.Text.Encoding.UTF8;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
byte[] newFileData = request.DownloadData(new Uri(ftpURI + fileName).ToString());
context.Response.ContentType = "application/download";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(newFileData);
// context.Response.BinaryWrite(System.IO.File.ReadAllBytes(RealFile));
context.Response.Flush();
// context.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();

其中

WebClient request = new WebClient();是微软官网的对象