C# .NET附件上传下载删除

其中最重要的一句代码是  string contentType = MimeMapping.GetMimeMapping(name);
这个是获取文件头信息的,不然下载了打不开。

下面代码是在内部共享服务器获取Url

  1   #region 附件
  2         public ActionResult Attachment(string type, string code)
  3         {
  4             ViewBag.code = code;
  5             ViewBag.result = GetAttachmentList(code);
  6             return View();
  7         }
  8         [HttpPost]
  9         [ValidateAntiForgeryToken]
 10         public DataTable GetAttachmentList(string code)
 11         {
 12             DataTable result = new DataTable();
 13             //查找路径是否存在,不存在则创建
 14             if (Directory.Exists("\\\\主目录\\次目录\\" + code) == false)
 15             {
 16                 Directory.CreateDirectory("\\\\主目录\\次目录\\" + code);
 17             }
 18             DirectoryInfo di = new DirectoryInfo("\\\\主目录\\次目录\\" + code);
 19             FileInfo[] infolist = di.GetFiles("*.*");
 20             //在内存表增加对应的列名
 21             result.Columns.Add("time", Type.GetType("System.String"));
 22             result.Columns.Add("name", Type.GetType("System.String"));
 23             result.Columns.Add("url", Type.GetType("System.String"));
 24             foreach (var info in infolist)
 25             {
 26                 DataRow newRow = result.NewRow();
 27                 newRow["time"] = info.CreationTime;
 28                 newRow["name"] = info.Name;
 29                 newRow["url"] = info.FullName;
 30                 result.Rows.Add(newRow);
 31 
 32             }
 33             return result;
 34         }
 35 
 36         /// <summary>
 37         /// 文件上传
 38         /// </summary>
 39         /// <param name="Attachment"></param>
 40         /// <param name="code"></param>
 41         /// <returns></returns>
 42         public ActionResult FileSelected(HttpPostedFileBase Attachment, string code)
 43         {
 44             if (Attachment != null)
 45             {
 46                 string fileName = Attachment.FileName;
 47                 fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
 48                 #region 判断文件夹是否存在,不存在先创建文件夹
 49                 if (Directory.Exists("\\\\主目录\\次目录\\" + code) == false)
 50                 {
 51                     Directory.CreateDirectory("\\\\主目录\\次目录\\" + code);
 52                 }
 53                 #endregion
 54                 Attachment.SaveAs("\\\\主目录\\次目录\\" + code + "\\" + fileName);
 55                 UIHelper.FileUpload("filePhoto").Reset();
 56             }
 57             List<string> fields = new List<string>
 58             {
 59                 "time",
 60                 "name",
 61                 "url"
 62             };
 63             UIHelper.Grid("AttachmentList").DataSource(GetAttachmentList(code), fields.ToArray());
 64             return UIHelper.Result();
 65         }
 66         #region 这边代码只做临时显示
 67         /// <summary>
 68         /// 附件下载
 69         /// </summary>
 70         /// <param name="url"></param>
 71         /// <param name="name"></param>
 72         /// <returns></returns>
 73         /// 这边是采用了 ValidateAntiForgeryToken验证,需要通过POST提交请求
 74         [ValidateAntiForgeryToken]
 75         public ActionResult AttachmentDownload(string url, string name)
 76         {
 77 
 78             //string contentType = MimeMapping.GetMimeMapping(name);
 79             //  FilePathResult a = File(url, contentType, name);
 80 
 81             Session.Add("FileDownLoad", new string[] { url, name });
 82             // return JavaScript("window.location.href=\"http://localhost:64475/货币.xlsx \"");
 83             //这边是通过 Url.Content 方法隐藏链接地址,这个 ~/Base/GetFileDownLoadBySession 不对,请放到对应的路由里面,这里只做临时显示
 84             return JavaScript("window.location.href='" + Url.Content("~/Base/GetFileDownLoadBySession") + "'");
 85         }
 86 
 87         /// <summary>
 88         /// 根据Session["FileDownLoad"] 获取到文件
 89         /// </summary>
 90         /// <returns></returns>
 91         ///这个是连接上面的,也请放到公共的路由里面,这里只做临时显示
 92         public ActionResult GetFileDownLoadBySession()
 93         {
 94             string[] fileDownLoad = Session["FileDownLoad"] as string[];
 95             Session.Remove("FileDownLoad");
 96             string contentType = MimeMapping.GetMimeMapping(fileDownLoad[1]);
 97             FilePathResult file = File(fileDownLoad[0], contentType, fileDownLoad[1]);
 98 
 99             return file;
100         } 
101         #endregion
102 
103 
104 
105 
106         /// <summary>
107         /// 附件删除
108         /// </summary>
109         /// <param name="url"></param>
110         /// <param name="code"></param>
111         /// <returns></returns>
112         [HttpPost]
113         [ValidateAntiForgeryToken]
114         public ActionResult AttachmentDelete(string url, string code)
115         {
116             System.IO.File.Delete(url);
117             List<string> fields = new List<string>
118             {
119                 "time",
120                 "name",
121                 "url"
122             };
123             UIHelper.Grid("AttachmentList").DataSource(GetAttachmentList(code), fields.ToArray());
124             return UIHelper.Result();
125         }
126         #endregion

 

posted @ 2019-09-06 16:41  Alex_Mercer  阅读(281)  评论(0)    收藏  举报