码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-06 11:58:55      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

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     }

 

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

标签:

原文地址:http://www.cnblogs.com/zzzq/p/4623603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!