代码
1 public ActionResult UploadFiles()
2 {
3 var r = new List<MikeUploadFile>();
4
5 foreach (string file in Request.Files)
6 {
7 HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
8 if (hpf.ContentLength == 0)
9 continue;
10 string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\" , Path.GetFileName(hpf.FileName));
11 hpf.SaveAs(savedFileName);
12
13 r.Add(new MikeUploadFile()
14 {
15 Name = savedFileName,
16 Length = hpf.ContentLength
17 });
18 }
19 return View(@"Upload", r);
20 }
21
2 {
3 var r = new List<MikeUploadFile>();
4
5 foreach (string file in Request.Files)
6 {
7 HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
8 if (hpf.ContentLength == 0)
9 continue;
10 string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\" , Path.GetFileName(hpf.FileName));
11 hpf.SaveAs(savedFileName);
12
13 r.Add(new MikeUploadFile()
14 {
15 Name = savedFileName,
16 Length = hpf.ContentLength
17 });
18 }
19 return View(@"Upload", r);
20 }
21
1 public class MikeUploadFile
2 {
3 public string Name { get; set; }
4 public int Length { get; set; }
5 }
2 {
3 public string Name { get; set; }
4 public int Length { get; set; }
5 }
很简单的代码就可以做到 单文件或多文件上传