ASP.NET--MVC实现文件上传下载

1.HTML页面上传下载

 1 <body>
 2         <div>
 3             <form action="/Default/File" enctype="multipart/form-data" method="post">
 4                 <input type="file" name="file" value="file" />
 5                 <input type="submit" value="上传" />
 6             </form>
 7         </div>
 8         <div>
 9             <a href="/Default/FileDownLoad/">下载</a>
10         </div>
11     </body>

2.控制器页面

 1         //上传
 2         public ActionResult File(HttpPostedFileBase file)
 3         {
 4             string path = Server.MapPath("~/Upload");
 5             string filename = Path.Combine(path, file.FileName);
 6             file.SaveAs(filename);
 7             return Content("成功");
 8         }
 9         public ActionResult FileDownLoad(string FileName)
10         {
11             //下载文件
12             string fileName = FileName;
13             string path = Server.MapPath("~/Upload/"+fileName);
14             FileStream fs = new FileStream(path, FileMode.Open);
15             return File(fs,"image/gif","a.jpg");
16         }

MVC实现文件上传下载就到这里了。

API+MVC实现文件上传下载

1.写一个文件上传下载的方法

 1 public FileResult UpLoad()
 2         {
 3             if (Request.Files.Count > 0)
 4             {
 5                 string FileUrlResult="";
 6                 foreach (string fn in Request.Files)
 7                 {
 8                     var file = Request.Files[fn];
 9                     var extenfilename = Path.GetExtension(file.FileName);
10                     //判断 路径是否存在
11                     string path = HttpContext.Current.Server.MapPath(UrlPath);
12                     if (!Directory.Exists(path))
13                     {
14                         Directory.CreateDirectory(path);
15                     }
16 
17                     if (ExtentsfileName.Contains(extenfilename.ToLower()))
18                     {
19                         string urlfile = UrlPath + DateTime.Now.ToFileTime() + extenfilename;
20                         string filepath = HttpContext.Current.Server.MapPath(urlfile);
21                         file.SaveAs(filepath);
22                         
23                     }
24                     else
25                     {
26                         return new FileResult() { Code = -1, Msg = "只允许上传指定格式文件" + string.Join(",", ExtentsfileName), Url = "" };
27                     }
28                 }
29                 return new FileResult() { Code = 0, Msg = "上传成功", Url = FileUrlResult };
30             }
31             else
32             {
33                 return new FileResult() { Code = -1, Msg = "不能上传空文件", Url = "" };
34             }
35         }
 1  public void DownLoad(string downFileName, string sourceFileName)//downFileName下载后保存名 spurceFileName服务器端物理路径
 2         {
 3             if (File.Exists(sourceFileName))
 4             {
 5                 Response.Clear();
 6                 Response.ClearHeaders();
 7                 Response.ClearContent();
 8                 Response.Buffer = true;
 9                 Response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", downFileName));
10                 Response.Charset = "GB2312";
11                 Response.ContentEncoding = Encoding.GetEncoding("GB2312");
12                 Response.ContentType = MimeMapping.GetMimeMapping(downFileName);
13                 Response.WriteFile(sourceFileName);
14                 Response.Flush();
15                 Response.Close();
16             }
17         }

 

2.API控制器

//上传
[HttpPost]
public FileResult UpLoad()
{
    return help.UpLoad();
}
//下载
[HttpGet]
public void DownLoad(string Url)
{
    string filePath = HttpContext.Current.Server.MapPath(Url);
    FileInfo fi = new FileInfo(filePath);
    help.DownLoad(fi.Name, fi.FullName);
}

 

3.客户端实现

 1 <input type = "file" id="f1" />
 2 <input type = "button" value="上传" onclick="ff()"/>  <a href="....../api/FileOperation/DownLoad?Url=/文件夹名/文件名">下载</a>
 3 <script>
 4  function ff()
 5 {
 6     var formData = new FormData();
 7     var file = document.getElementById("f1").files[0];
 8     formData.append("fileInfo", file);
 9         $.ajax({
10     url: "....../api/FileOperation/UpLoad",
11             type: "POST",
12             data: formData,
13             contentType: false,//必须false才会自动加上正确的Content-Type
14             processData: false,//必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理
15             success: function(data) {
16             if (data.Code < 0)
17                 alert(data.Msg)
18                 else
19                 alert(data.Url)
20             },
21             error: function(data) {
22             alert("上传失败!");
23         }
24     });
25 }
26 
27 </script>

 


 posted on 2020-07-22 15:33  奇怪的小知识  阅读(52)  评论(0)    收藏  举报