ASP.NET 5探险(2):上传文件

时间:2023-01-25 10:44:12

(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注。)

题记:在ASP.NET 5(MVC 6)中处理上传文件的方式和之前有所不同。

在MVC 5之前的版本中上传文件,有两种方式。

1,直接对Request.Files进行访问获取HttpPostedFileBase,如下面代码所示:

[HttpPost]
public ActionResult Upload()
{
string path = @"D:\Temp\"; HttpPostedFileBase photo = Request.Files["photo"]; if(photo != null)
photo.SaveAs(path + photo.FileName); return RedirectToAction("Index");
}

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 2,通过模型绑定获取HttpPostedFileBase,如下面代码所示:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase photo)
{
string path = @"D:\Temp\"; if(photo != null)
photo.SaveAs(path + photo.FileName); return RedirectToAction("Index");
}

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 更多详细的用法可以参考CodeProject的这篇文章《Uploading and returning files in ASP.NET MVC》。

在MVC 6中同样也是有两种方式,只是提供的类和之前有所不同,没有HttpPostedFileBase,取而代之的是IFormFile,且额外的一些文件信息放到了ContentDispositionHeaderValue。

1,直接使用Request.Form.Files访问IFormFile,如下代码所示:

[HttpPost]
public ActionResult Upload()
{
string path = @"D:\Temp\"; IFormFile photo = Request.Form.Files["photo"]; if (photo != null)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(photo.ContentDisposition);
var originalName = parsedContentDisposition.FileName.Replace("\"", "");
photo.SaveAs(path + originalName);
} return RedirectToAction("Index");
}

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

大家可以注意到了,我对parsedContentDisposition.FileName进行了一个替换处理。这是因为FileName这个属性值会带有前后双引号。我不知道这个是个bug还是故意设计,过一会去提个Issue问问。

2,通过模型绑定获取IFormFile,如下面代码所示:

[HttpPost]
public ActionResult Upload(IFormFile photo)
{
string path = @"D:\Temp\"; if (photo != null)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(photo.ContentDisposition);
var originalName = parsedContentDisposition.FileName.Replace("\"", "");
photo.SaveAs(path + originalName);
} return RedirectToAction("Index");
}

另外,还需要注意的是,在MVC 6中已经无法用Server.MapPath这样的方法来得到虚拟地址对应的物理地址了,只能通过IHostingEnvironment.MapPath来获取(此方法是一个扩展方法)。而要使用IHostingEnvironment的实例,必须把其注入到Controller中(IHostingEnvironment是默认被系统注册的,无需也无法显式注册)。经过我的试验,只能通过构造器注入,而无法通过[Activate]来属性注入。