jquery easyui filebox 上傳附件 + asp.net后台


form必須加這個屬性enctype="multipart/form-data",否則后台獲取不到文件

 1 <script>
 2 function uploadFiles() {
 3     $('#fm').form('submit', {                
 4                 url: 'Upload.aspx?fjssmk=Xmgl',
 5                 success: function (result) {
 6                     var result = eval('(' + result + ')');
 7                     //可以寫一些提示的代碼等等..
 8                 }
 9             });
10 }
11 </script>
1 <form id="fm" method="post" enctype="multipart/form-data">
2     <input id="upload1" class="easyui-filebox" name="file1" data-options="prompt:'選擇文件...',onChange:function(){uploadFiles()}" style="width: 150px" />
3 </form>

 asp.net 后台代碼 , 其中Sys_fjxx為一個Model文件, 可以去掉 , 對數據庫的操作改成你自己的就可以了, 

 1 public class Upload : IHttpHandler, IRequiresSessionState
 2     {
 3 
 4         public bool IsReusable
 5         {
 6             get { return false; }
 7         }
 8 
 9         public void ProcessRequest(HttpContext context)
10         {
11             try
12             {
13                 string fjssmk = context.Request["fjssmk"];
14                 string userid = Utility.GetCurrentUser().zybh + "";
15 
16                 HttpFileCollection httpFileCollection = context.Request.Files;
17                 HttpPostedFile file = null;
18                 if (httpFileCollection.Count > 0)
19                     file = httpFileCollection[0];
20                 if (file != null)
21                 {
22                     DBHelp.DBExecute exe = new DBHelp.DBExecute();
23                     Sys_fjxx fjxx = ConvertFile(file);
24                     fjxx.ssmk = fjssmk;
25                     fjxx.xdlj = "/UploadFiles/" + fjssmk + "/" + DateTime.Today.ToString("yyyy-MM-dd");
26                     fjxx.scrbh = int.Parse(userid);
27                     //
28                     SaveFile(fjxx.xdlj, fjxx.fwqwjm, file);
29                     exe.Add(fjxx);
30                     exe.DBCommit();
31                     string url = fjxx.xdlj + "/" + fjxx.fwqwjm;
32                     context.Response.Write("{\"fjbh\":\"" + fjxx.fjbh + "\",\"url\":\"" + url + "\",\"error\":0}");//
33                 }
34             }
35             catch (Exception exp)
36             {
37                 context.Response.Clear();
38                 context.Response.Write("{\"error\":\"" + exp.Message + "\"}");
39             }
40             finally
41             {
42                 context.Response.Flush();
43                 context.Response.End();
44             }
45         }
46         /// <summary>
47         /// 轉成Sys_fjxx
48         /// </summary>
49         /// <param name="file"></param>
50         /// <returns></returns>
51         private Sys_fjxx ConvertFile(HttpPostedFile file)
52         {
53             FileInfo fi = new FileInfo(file.FileName);
54             Sys_fjxx fjxx = new Sys_fjxx();
55             fjxx.fjbh = Lsh.Service.GetLsh("Sys_fjxx", "fjbh");
56             fjxx.wjm = (fi.Name + "//").Replace(fi.Extension + "//", "");
57             fjxx.kzm = fi.Extension;
58             fjxx.wjdx = Convert.ToDecimal(file.ContentLength) / 1024;
59             fjxx.fwqwjm = DateTime.Now.Ticks + fjxx.kzm;
60             fjxx.scsj = DateTime.Now;
61             return fjxx;
62         }
63 
64         private void SaveFile(string path, string filename, HttpPostedFile file)
65         {
66             if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
67             {
68                 Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
69             }
70             file.SaveAs(HttpContext.Current.Server.MapPath(path + "/" + filename));
71         }
72     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM