引用js文件包:jquery.form.js可以下载 http://malsup.com/jquery/form/#download
var formEle = $("#DefaultPicture_Create_Form");//一定要form.find不然与查询页面重复 var defaultPictureName = formEle.find("#DefaultPictureName").val().trim(); var defaultPictureID = formEle.find("#DefaultPictureID").val(); $("#DefaultPicture_Create_Form").ajaxSubmit({ url: "@Url.Action("Save", "DefaultPictureMaintenance")", type: "post", dataType: 'json', data: { DefaultPictureName: defaultPictureName, DefaultPictureID: defaultPictureID }, beforeSend: function () { showLoading(); }, success: function (data) { debugger if (data.Status == 200) { showOkClose(data.Message); closePopup(); DefaultPictureSearch(); } else { showError(data.Message); } }, error: function (aa) { debugger; alert(aa); }, complete: function () { hideLoading(); } });
////// 将 Stream 转成 byte[] /// /// ///private byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } /// /// 检查文件是否合格,不合格返回错误信息,合格返回空字符 /// /// ///private string CheckUploadFile(HttpPostedFileBase file) { //判断文件必须小于2M 格式必须PNG JPG if (file.ContentType != "image/png" && file.ContentType != "image/jpeg") { return "Upload failed! Picture type can only be JPG or PNG."; } if (file.ContentLength / 1024 > 1024 * 2) { return"Upload failed! Image size can not be greater than 2M."; } return ""; } public ActionResult Save(int DefaultPictureID, string DefaultPictureName, HttpPostedFileBase file) { bool isSaveSuccess = true; var DefaultPicture = new DefaultPictureItem(); if (DefaultPictureID > 0)//编辑 { //编辑时可以运行File为Null,表示用户没有修改图片 var defaultPicModel = _defaultPictureService.GetByID(DefaultPictureID);//获取数据库中的 defaultPicModel.DefaultPictureName = DefaultPictureName; defaultPicModel.UpdateBy = "testUser"; defaultPicModel.UpdateDate = DateTime.Now; if (file!=null) { //判断文件必须小于2M 格式必须PNG JPG var errorMsg = CheckUploadFile(file); if (!string.IsNullOrEmpty(errorMsg)) { return Error(errorMsg); } var fileStream = file.InputStream; defaultPicModel.DefaultPictureContent = StreamToBytes(fileStream); DefaultPicture.DefaultPictureHaskKey = Guid.NewGuid().ToString(); } isSaveSuccess = _defaultPictureService.Update(defaultPicModel); } else { //新增 if (file == null)//新增的时候文件内容是必须得 { return Error("Upload failed! Image content can not be empty"); } DefaultPicture.DefaultPictureName = DefaultPictureName; DefaultPicture.DefaultPictureHaskKey = Guid.NewGuid().ToString(); DefaultPicture.CreateBy = "testUser"; DefaultPicture.CreateDate = DateTime.Now; DefaultPicture.UpdateBy = "testUser"; DefaultPicture.UpdateDate = DateTime.Now; //获取文件的内容 //判断文件必须小于2M 格式必须PNG JPG var errorMsg = CheckUploadFile(file); if (!string.IsNullOrEmpty(errorMsg)) { return Error(errorMsg); } var fileStream = file.InputStream; DefaultPicture.DefaultPictureContent = StreamToBytes(fileStream); isSaveSuccess = _defaultPictureService.Update(DefaultPicture); } if (isSaveSuccess) { return Success("Save Success"); } else { return Error("Save Failed"); } }