.Net MVC FTP文件下载
来源:互联网 发布:华傲数据是外包公司么 编辑:程序博客网 时间:2024/06/08 10:35
前台通过<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 try17 {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 else41 {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 0
- .Net MVC FTP文件下载
- .net下载ftp文件
- Asp.Net Mvc下载文件
- 利用apach net 组件 下载 文件(ftp)
- sun.net.ftp.FtpClient下载、上传文件
- asp.net 文件下载,同样适用于mvc
- .net MVC 下载文件乱码问题解决方案
- asp.net mvc如何实现下载文件
- .NET MVC实现文件下载的实例
- asp.net mvc如何实现文件下载
- asp.net mvc + javascript生成下载文件
- .Net MVC 文件上传和下载
- ASP.NET MVC 服务器端下载文件
- ASP.NET MVC 远程下载文件
- .Net MVC后台接收xheditor上传文件,FTP文件传输
- ASP.NET 使用FTP文件上传、下载文件
- asp.net mvc 下载文件并不打开下载页面
- .NET下的FTP上传,下载文件(支持中文名)
- windows下为mysql设置密码
- uplodify上传成功后,删除文件
- RPM 文件安装过程中 最后加上的--nodeps 和--force参数是什么意思
- C++第二次试验作业
- python压缩解压缩相关
- .Net MVC FTP文件下载
- Dagger2在AndroidStudio中的应用以及MVP
- 注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210
- C++学习笔记
- NetBeans doc path
- zctf-pwn500-restaurant-write-up
- uploadify报错,自动请求框架的url地址
- github反应过慢,css加载失败问题
- Spark是否会替代Hadoop?