js:
$(document).ready(function () { //$('#creater').combobox({ // url: '/VMS.UI/BindData/ScheamData?type=26', // dataType: 'json' //}); $('#education').combobox({ url: '/VMS.UI/BindData/ScheamData?type=26', dataType: 'json' }); $('#job').combobox({ url: '/VMS.UI/BindData/ScheamData?type=24', dataType: 'json', value: '专职' }); $('#station').combobox({ url: '/VMS.UI/BindData/ScheamData?type=29', dataType: 'json' }); $('#org').combotree({ url: '/VMS.UI/BindData/OrgData', dataType: 'json', idFiled: 'IID', textFiled:'OrgName', onLoadSuccess: function () { $('#org').combotree('tree').tree("collapseAll"); }, onSelect: function (node) { $('#dept').combobox({ url: '/VMS.UI/BindData/GetDepartments?deptID=' + node.id, dataType: 'json', valueField: 'IID', textField: 'DeptName' }); } }); $(function () { $("#uploadpic").uploadPreview({ Img: "pic", Width: 120, Height: 120 }); }); function formValidate() { var station = $('#station').combobox('isValid'); var name = $('#name').validatebox('isValid'); var cardNumber = $('#cardNumber').validatebox('isValid'); var mobilePhone = $('#mobilePhone').numberbox('isValid'); var org = $('#org').combotree('isValid'); var dept = $('#dept').combobox('isValid'); var drivingCertificate = $('input[name="DrivingCertificate"]').validatebox('isValid'); if (!station) { $('#station').combobox().next('span').find('input').focus() return false; } if (!name) { $('#name').focus(); return false; } if (!cardNumber) { $('#cardNumber').focus() return false; } if (!mobilePhone) { $('#mobilePhone').focus() return false; } if (!org) { $('#org').combobox().next('span').find('input').focus() return false; } if (!dept) { $('#dept').combobox().next('span').find('input').focus() return false; } if (!drivingCertificate) { $('input[name="DrivingCertificate"]').focus() } return true; } $('#save').click(function () { if (formValidate()) { $('#form_driverinfo').submit(); } }); $('#clear').click(function () { $.messager.confirm('确认对话框', '是否确定清空?', function (r) { if (r) { $('#form_driverinfo').form('reset'); } }); }); });
MVC:
[HttpPost] public ActionResult AddVehicleDriver(VehicleDrivers driver) { if (driver.Picture != null) { HttpPostedFileBase file = Request.Files["Picture"]; if (file != null) { driver.Picture = SaveImgAndGetPath(file); } } } /// <summary> /// 保存图片并获取地址 /// </summary> /// <param name="file">文件</param> /// <returns>返回路径</returns> private string SaveImgAndGetPath(HttpPostedFileBase file) { //设置文件名+获取文件扩展名 string imgName = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999).ToString() + Path.GetExtension(file.FileName); // 生成要存档的 文件路径和文件名 string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); string imgFullPath = Path.Combine(serverPath, @"UploadImage\driverImg\", imgName); //string imgPath = Path.Combine(Server.MapPath("/UploadImage/driverImg/"), imgName); //上传服务器 file.SaveAs(imgFullPath); string imgRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath + @"\UploadImage\driverImg\" + string.Format("{0}", imgName); return imgRelativePath; }
jqueryExtends:
jQuery.fn.extend({ uploadPreview: function (opts) { var _self = this, _this = $(this); opts = jQuery.extend({ Img: "ImgPr", Width: 100, Height: 100, ImgType: ["gif", "jpeg", "jpg", "bmp", "png"], Callback: function () { } }, opts || {}); _self.getObjectURL = function (file) { var url = null; if (window.createObjectURL != undefined) { url = window.createObjectURL(file) } else if (window.URL != undefined) { url = window.URL.createObjectURL(file) } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file) } return url }; _this.change(function () { if (this.value) { if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) { alert("选择文件错误,图片类型必须是" + opts.ImgType.join(",") + "中的一种"); this.value = ""; return false } if ($.browser.msie) { try { $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0])) } catch (e) { var src = ""; var obj = $("#" + opts.Img); var div = obj.parent("div")[0]; _self.select(); if (top != self) { window.parent.document.body.focus() } else { _self.blur() } src = document.selection.createRange().text; document.selection.empty(); obj.hide(); obj.parent("div").css({ 'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)', 'width': opts.Width + 'px', 'height': opts.Height + 'px' }); div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src } } else { $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0])) } opts.Callback() } }) } });