用普通的ajax提交表单的时候,不能把文件流传到后端去,所以要用到jquery.form.js
jquery.form.js到官网下载或者从这里下载:http://pan.baidu.com/s/1c2JS60C
引用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");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)