一次上传多个文件

上传界面

1、前台代码

 1     <div class="contain_bj" style="background: none; border: 0px; padding: 0px; width: 980px;">
 2         <!---采购合同-->
 3         @using (Html.BeginForm("AddContractSubmit", "OrderDraft", FormMethod.Post, new { enctype = "multipart/form-data", @id = "ContractForm" }))
 4         {
 5             <table id="table_measure" border="0" cellspacing="0" cellpadding="0" width="100%"
 6                 class="contain_form contain_form_bd">
 7                 <tr>
 8                     <th colspan="6">
 9                         <h2>
10                             合同模板</h2>
11                     </th>
12                 </tr>
13                 <tr class="class_pt">
14                     <td align="center" width="20%">
15                         采购合同模板
16                     </td>
17                     <td colspan="5" align="left" width="80%" style="width: 400px">
18                         <input type="file" name="filetemplate" style="width: 300px" />
19                     </td>
20                 </tr>
21                 <tr class="class_pt">
22                     <td align="center" width="20%">
23                         其他附件<img title="添加" alt="" src="../../Content/images/jia.gif" style="cursor: pointer;"
24                             onclick="AddFuJian()" />
25                     </td>
26                     <td colspan="5">
27                         <ul style="width: 600px">
28                             <li>
29                                 <input type="file" name="fileother" style="width: 300px" />
30                                 <img title="删除附件" src="../../Content/images/jian.gif" style="cursor: pointer;" onclick="DetFuJian(this)" />
31                             </li>
32                             <li>
33                                 <input type="file" name="fileother" style="width: 300px" />
34                                 <img title="删除附件" src="../../Content/images/jian.gif" style="cursor: pointer;" onclick="DetFuJian(this)" />
35                             </li>
36                             <li>
37                                 <input type="file" name="fileother" style="width: 300px" />
38                                 <img title="删除附件" src="../../Content/images/jian.gif" style="cursor: pointer;" onclick="DetFuJian(this)" />
39                             </li>
40                             <li id="tr_mas"></li>
41                         </ul>
42                     </td>
43                 </tr>
44             </table>
45             <div class="form_button">
46                 <a>上一步</a> <a onclick="SaveNext()">下一步</a>
47             </div>
48 
49         }
50     </div>

2、后台接收

 1         [HttpPost]
 2         public ActionResult AddContractSubmit(HttpPostedFileBase filebase)
 3         {
 4 
 5             if (Request.Files.Count == 0)
 6             {
 7                 return View("AddContract");
 8             }
 9 
10             List<AttachFileView> listFile = new List<AttachFileView>();
11 
12             for (int i = 0; i < Request.Files.Count; i++)
13             {
14                 AttachFileView fileView = new AttachFileView();
15 
16                 var file = Request.Files[i];
17                 if (file.ContentLength == 0)
18                 {
19                     continue;
20                 }
21                 else
22                 {
23                     //判断上传附件的类型
24                     if (Request.Files.AllKeys[i] == "filetemplate")
25                         fileView.FILETYPE = "TEMPLATE";
26                     else
27                         fileView.FILETYPE = "OTHER";
28 
29                     fileView.FILE = file;
30                     fileView.FILEPATH = Server.MapPath(Request.ApplicationPath) + "/FileUpload/";
31                 }
32                 listFile.Add(fileView);
33 
34             }
35 
36             string ordercode = "ORDER20130803FH";//】【】【】【】【】【】【】
37 
38 
39             OperationResults message = orderBLL.SaveFile(listFile, ordercode);
40             //上传、数据库保存成功
41             if (message.Code == 1)
42             {
43                 return View();
44             }
45             else
46             {
47                 return View("AddContract");
48 
49             }
50 
51         }

3、业务处理

 1         public OperationResults SaveFile(List<AttachFileView> listFile, string ordercode)
 2         {
 3             List<AttachFileView> listAttachFile = new List<AttachFileView>();
 4 
 5             string type = "ORDER"; //单据类型
 6             string date = DateTime.Now.ToString("yyyyMMdd");
 7 
 8             //遍历集合,保存上载文件
 9             foreach (var item in listFile)
10             {
11 
12                 //不包含扩展名的文件名
13                 string noExFileName = Path.GetFileNameWithoutExtension(item.FILE.FileName);
14                 //文件扩展名
15                 string fileEx = Path.GetExtension(item.FILE.FileName);
16                 //保存时的文件名
17                 Guid guid = Guid.NewGuid();
18                 string fileName = guid.ToString() + noExFileName + date + fileEx;
19                 //导入服务器的地址路径
20                 string dir = item.FILEPATH  + type + "/" + date + "/";
21                 //如果要保存的目录不存在
22                 if (!Directory.Exists(dir))
23                 {
24                     Directory.CreateDirectory(dir);  //创建文件所在目录
25                 }
26                 string savePath = Path.Combine(dir, fileName);
27 
28                 //保存上载文件
29                 item.FILE.SaveAs(savePath);
30 
31                 AttachFileView fileView = new AttachFileView();
32                 fileView.UID = guid;
33                 fileView.FILETYPE = item.FILETYPE;
34                 fileView.FILENAME = noExFileName;
35                 fileView.FILEPATH = savePath;
36                 listAttachFile.Add(fileView);
37             }
38 
39             //向数据库中保存上传附件信息
40             List<TS_ATTACHFILE> listTs = new List<TS_ATTACHFILE>();
41             foreach (var item in listAttachFile)
42             {
43                 item.FORMCODE = type;
44                 item.BUSINESSCODE = ordercode;
45                 item.LASTTIME = DateTime.Now;
46 
47                 TS_ATTACHFILE attachfile = DataModelHepler.GetDataModel<AttachFileView, TS_ATTACHFILE>(item);
48 
49                 listTs.Add(attachfile);
50             }
51             OperationResults message = new OperationResults();
52             message.ResultCount = dal.insertBatch<TS_ATTACHFILE>(listTs);
53 
54             if(message.ResultCount == 0)
55             {
56                 message.Code = 0;
57                 message.Message = "保存失败!";
58             }
59             else
60             {
61                 message.Code = 1;
62                 message.Message = "保存成功!";
63             }
64 
65             return message;
66 
67         }

 

 

posted @ 2013-08-08 17:35  疯子艾云  阅读(273)  评论(0编辑  收藏  举报