在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前端: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title></title> 6 </head> 7 <body> 8 <input type="file" id="myphoto" /> 9 <div style="display:none" id="div_div1"> 10 <img src="#" id="img_myphoto" /> 11 </div> 12 </body> 13 </html> 14 <script src="assets/js/jquery-1.7.1.js"></script> 15 <script type="text/javascript"> 16 $("#myphoto").change(function () { 17 var file = this.files[0]; 18 var formData = new FormData(); 19 formData.append('tFile', file); 20 jQuery.support.cors = true; 21 $.ajax({ 22 type: "POST", // 必须用post 23 url: "http://118.200.100.100:8080/uploadStudentIDCard.ashx?studentID=100", 24 crossDomain: true, 25 data: formData, 26 contentType: false, 27 processData: false, 28 success: function (data) { 29 if (data.result == 1) { 30 $("#img_myphoto").attr("src", "http://114.215.198.241:8086" + data.url); 31 $("#div_div1").show(); 32 } 33 }, 34 error: function (res) { 35 alert('shit'); 36 } 37 }); 38 }); 39 </script> 后端: 1 using JSXiangYu.JiSuJob.BLLFactory; 2 using JSXiangYu.JiSuJob.IBLL; 3 using JSXiangYu.JiSuJob.Utility; 4 using System; 5 using System.Collections.Generic; 6 using System.Drawing; 7 using System.IO; 8 using System.Linq; 9 using System.Web; 10 11 namespace JSXiangYu.JiSuJob.WebApp.student.person 12 { 13 public class ResultData 14 { 15 public int _result; 16 public string _msg; 17 } 18 /// <summary> 19 /// uploadStudentIDCard 的摘要说明 20 /// </summary> 21 public class uploadStudentIDCard : IHttpHandler 22 { 23 IBLLSession bllSession = BLLSessionFactory.CreateBLLSession();//获取业务逻辑会话对象 24 public void ProcessRequest(HttpContext context) 25 { 26 context.Response.ContentType = "Application/json"; 27 28 UploadStudentIDCard(context); 29 } 30 31 /// <summary> 32 /// 上传学生证 33 /// </summary> 34 /// <param name="context"></param> 35 private void UploadStudentIDCard(HttpContext context) 36 { 37 context.Response.AddHeader("Access-Control-Allow-Origin", "*"); 38 if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 39 { 40 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); 41 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept,X-Requested-With"); 42 HttpContext.Current.Response.End(); 43 } 44 45 //1.采集数据 46 //string kk = context.Request.Params["jsoncallback"]; 47 string studentIDStr = context.Request.Params["studentID"];//学生编号 48 HttpPostedFile file = context.Request.Files["tFile"];//学生证 49 //2.数据校验 50 int studentID; 51 if (!int.TryParse(studentIDStr, out studentID)) 52 { 53 context.Response.Write("{\"result\":2}");//学生编号格式有误(学生未登录) 54 context.Response.End(); 55 return; 56 } 57 if (file == null) 58 { 59 context.Response.Write("{\"result\":3}");//未上传学生证 60 context.Response.End(); 61 return; 62 } 63 64 //3.处理业务逻辑 65 //3.1处理存储文件的路径 66 string fileName = Path.GetFileName(file.FileName);//文件的全名称 67 string ext = fileName.Substring(fileName.LastIndexOf('.'));//文件的扩展名 68 string dateBasePath;//保存身份证的相对路径 69 string dateBasePath_m;//手机展示相对路径 70 string path_m;//手机展示物理路径 71 string guidStr = Guid.NewGuid().ToString() + ext;//图片名称用Guid命名 72 //3.2物理路径 73 string path = GetUploadFilePath(out dateBasePath, out dateBasePath_m, out path_m) + guidStr;//物理路径+文件全名称 74 path_m += guidStr;//物理路径——手机端 75 //3.3相对路径 76 dateBasePath += guidStr;//相对路径+文件全名称 77 dateBasePath_m += guidStr;//相对路径——手机端 78 //3.4处理图片+上传图片 79 Bitmap bmBig = new Bitmap(file.InputStream); 80 FileExt.MakeThumbnail(bmBig, path, 1024, 600); 81 Bitmap bmBig2 = new Bitmap(file.InputStream); 82 FileExt.MakeThumbnail(bmBig2, path_m, 336, 240); 83 //3.5判断学生账号是否存在 84 IStudentServer _studentServer = bllSession.StudentServer; 85 if (!_studentServer.IsExistStudentAccount(studentID)) 86 { 87 context.Response.Write("{\"result\":4}");//不存在该学生账号 88 context.Response.End(); 89 return; 90 } 91 //3.6更新学生信息 92 if (_studentServer.UpdateStudentInfoOfStudentIDCard(studentID, dateBasePath, dateBasePath_m)) 93 { 94 context.Response.Write("{\"result\":1,\"url\":\"" + dateBasePath_m + "\"}");//图片上传成功 95 context.Response.End(); 96 return; 97 } 98 else 99 { 100 context.Response.Write("{\"result\":0}");//图片上传失败 101 context.Response.End(); 102 return; 103 } 104 } 105 106 107 /// <summary> 108 /// 处理保存学生证的路径 109 /// </summary> 110 /// <param name="newPath">相对路径</param> 111 /// <returns>返回物理路径</returns> 112 private string GetUploadFilePath(out string newPath, out string dateBasePath_m, out string path_m) 113 { 114 DateTime date = DateTime.Now; 115 string tempPath = date.Year + "/" + date.Month + "/" + date.Day + "/"; 116 117 string oldPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets/img/student/studentIDCard/");//原始路径 118 oldPath += tempPath;//压缩图片物理路径 119 120 path_m = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets/img/student/studentIDCard_m/");//原始路径 121 path_m += tempPath;//压缩图片物理路径——手机端 122 123 if (!Directory.Exists(oldPath)) 124 { 125 Directory.CreateDirectory(oldPath); 126 } 127 if (!Directory.Exists(path_m)) 128 { 129 Directory.CreateDirectory(path_m); 130 } 131 newPath = "/assets/img/student/studentIDCard/" + tempPath;//压缩图片相对路径 132 dateBasePath_m = "/assets/img/student/studentIDCard_m/" + tempPath;//压缩图片相对路径——手机端 133 return oldPath; 134 } 135 136 public bool IsReusable 137 { 138 get 139 { 140 return false; 141 } 142 } 143 } 144 }
|
请发表评论