基於asp.net MVC的無重新整理檔案上傳
來源:互聯網
上載者:User
Code
1 <div style="float:left;">
2 <div style="position: absolute; z-index: -1; background-color: Gray; height: 25px;
3 line-height: 25px; width: 68px; text-align:center; color:White; cursor:pointer;">
4 添加檔案
5 </div>
6 <div style="height: 25px; width: 68px;line-height: 25px;cursor:pointer;">
7 <input type="file" id="File1" name="File1" style="line-height: 25px; background-color: Red;
8 height: 25px; width: 68px; filter: Alpha(opacity=0); -moz-opacity: 0.0; opacity: 0.0;
9 z-index: 1; cursor:pointer;" onchange="text.value=this.value;" />
10 </div>
11 </div>
12
13 <input type="text" id="text" style="height:24px; line-height:24px; background-color:Gray; color:White;"/>
14 <input type="button" onclick="upload_Click();" value="上傳" style="height:25px; line-height:25px;"/>
15
16
17 <div style="display:none; position:absolute; top:0; left:0;">
18 <iframe name="uploadResponse"></iframe>
19 <% using (Html.BeginForm("upload", "AQ", FormMethod.Post, new { enctype = "multipart/form-data", target = "uploadResponse", id = "load" }))
20 {%>
21 <%} %>
22 </div>
23
24 <script type="text/javascript">
25 function upload_Click() {
26 var imgObj = document.getElementById("File1");
27 var from1 = document.getElementById("load");
28 from1.appendChild(imgObj);
29 from1.submit();
30 }
31 function ss() {
32 alert("上傳成功");
33 }
34 </script>Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using System.Web.Mvc.Ajax;
7 using Medusa.Mow.BusinessRule;
8 using Medusa.Mow.DataModel;
9 namespace MedusaOfficialWeb.Controllers
10 {
11 [HandleError]
12 public class AQController : PageController
13 {
14 public ActionResult Index()
15 {
16 return View();
17 }
18
19 public string upload()
20 {
21 HttpFileCollectionBase files = HttpContext.Request.Files;
22 for (int iFile = 0; iFile < files.Count; iFile++)
23 {
24 HttpPostedFileBase postedFile = files[iFile];
25 string fileName = System.IO.Path.GetFileName(postedFile.FileName);
26 if (fileName != null && fileName != string.Empty)
27 {
28 postedFile.SaveAs(Server.MapPath("~/file/" + DateTime.Now.Ticks.ToString() + fileName));
29 }
30 }
31
32 Response.Write(string.Format("<script type='text/javascript'>window.parent.ss();</script>"));
33 return string.Empty;
34 }
35 }
36 }
37