.Net MVC FTP文件下载

前台通过<a>标签实现文件下载:

代码为:

1 window.open("/home/downloadAttachfile?filename=" + "文件名称");

效果为打开新的tab页面实现下载;

后台HomeController处理请求:

 1  public FileContentResult downloadAttachfile()
 2         {
 3             string ftpServerIP = asolution.config.Object.FileServers.FindByName("ftp").Host; //本次项目框架web.config的FTP的ip和用户名,密码配置信息
 4             string ftpUserID = asolution.config.Object.FileServers.FindByName("ftp").Uid;
 5             string ftpPassword = asolution.config.Object.FileServers.FindByName("ftp").Pwd;
 6             string fileName = Server.UrlDecode(Request.Params["filename"]);
 7             string uri = ftpServerIP + "/attachFiles/" + fileName;
 8             Uri serverUri = new Uri(uri);
 9 
10             // Get the object used to communicate with the server.
11             WebClient request = new WebClient();
12 
13             // This example assumes the FTP site uses anonymous logon.
14             request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
15             byte[] newFileData;
16             try
17             {
18                 newFileData = request.DownloadData(serverUri.ToString()); //从FTP服务器下载文件
19                 string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
20                 Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
21                 string fileType = fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - 1 - fileName.LastIndexOf('.'));
23                 string contenttype = "application/";
24                 if (fileType == ".text")   //根据文件类型判断类型头
25                 {
26                     contenttype = "text/plain";
27                 }
28                 else if (fileType == ".doc" || fileType == ".docx")
29                 {
30                     contenttype += "msword";
31                 }
32                 else if (fileType == ".xls" || fileType == ".xlsx" || fileType == ".ppt" || fileType == ".pptx")
33                 {
34                     contenttype = contenttype + "x-" + fileType.Substring(1);
35                 }
36                 else if (fileType == ".jpg" || fileType == "bmp" || fileType == "jpeg" || fileType == "gif" || fileType == "png")
37                 {
38                     contenttype = "image/" + fileType.Substring(1);
39                 }
40                 else
41                 {
42                     contenttype += "x-" + fileType.Substring(1);
43 
44                 }
45                 return File(newFileData, contenttype);
46             }
47             catch (WebException e)
48             {
49                 // 若服务器未开启或者文件不存在抛出异常
50                 Response.Write("<script languge='javascript'>alert('源文件已经被删除或者文件服务器未开启!'); window.close(); </script>");
51                 return null;
52             }
53         }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值