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