引用js文件包:jquery.form.js可以下载 http://malsup.com/jquery/form/#download
<script src="script/jquery.form.js"></script>
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(); } });
/// <summary> /// 将 Stream 转成 byte[] /// </summary> /// <param name="stream"></param> /// <returns></returns><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] StreamToBytes(Stream stream) { </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[stream.Length]; stream.Read(bytes, </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, bytes.Length); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置当前流的位置为流的开始 </span> stream.Seek(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, SeekOrigin.Begin); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bytes; } </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span> <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 检查文件是否合格,不合格返回错误信息,合格返回空字符 </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="file"></param></span> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> CheckUploadFile(HttpPostedFileBase file) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断文件必须小于2M 格式必须PNG JPG</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (file.ContentType != <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">image/png</span><span style="color: rgba(128, 0, 0, 1)">"</span> && file.ContentType != <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">image/jpeg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Upload failed! Picture type can only be JPG or PNG.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (file.ContentLength / <span style="color: rgba(128, 0, 128, 1)">1024</span> > <span style="color: rgba(128, 0, 128, 1)">1024</span> * <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Upload failed! Image size can not be greater than 2M.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> ActionResult Save(<span style="color: rgba(0, 0, 255, 1)">int</span> DefaultPictureID, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> DefaultPictureName, HttpPostedFileBase file) { </span><span style="color: rgba(0, 0, 255, 1)">bool</span> isSaveSuccess = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">var</span> DefaultPicture = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DefaultPictureItem(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (DefaultPictureID > <span style="color: rgba(128, 0, 128, 1)">0</span>)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">编辑</span> { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">编辑时可以运行File为Null,表示用户没有修改图片</span> <span style="color: rgba(0, 0, 255, 1)">var</span> defaultPicModel = _defaultPictureService.GetByID(DefaultPictureID);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取数据库中的</span> defaultPicModel.DefaultPictureName =<span style="color: rgba(0, 0, 0, 1)"> DefaultPictureName; defaultPicModel.UpdateBy </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">testUser</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; defaultPicModel.UpdateDate </span>=<span style="color: rgba(0, 0, 0, 1)"> DateTime.Now; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (file!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断文件必须小于2M 格式必须PNG JPG</span> <span style="color: rgba(0, 0, 255, 1)">var</span> errorMsg =<span style="color: rgba(0, 0, 0, 1)"> CheckUploadFile(file); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.IsNullOrEmpty(errorMsg)) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Error(errorMsg); } </span><span style="color: rgba(0, 0, 255, 1)">var</span> fileStream =<span style="color: rgba(0, 0, 0, 1)"> file.InputStream; defaultPicModel.DefaultPictureContent </span>=<span style="color: rgba(0, 0, 0, 1)"> StreamToBytes(fileStream); DefaultPicture.DefaultPictureHaskKey </span>=<span style="color: rgba(0, 0, 0, 1)"> Guid.NewGuid().ToString(); } isSaveSuccess </span>=<span style="color: rgba(0, 0, 0, 1)"> _defaultPictureService.Update(defaultPicModel); } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新增</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (file == <span style="color: rgba(0, 0, 255, 1)">null</span>)<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新增的时候文件内容是必须得</span>
{
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");
}
}