新增

new { enctype = "multipart/form-data" } 这个必须要有
@using (Html.BeginForm(Html.BeginForm("Create", "UserInfo", FormMethod.Post, new { enctype = "multipart/form-data" })))
{ <div class="editor-label">
<span>图片</span>
</div>
<div class="editor-field">
<input type="file" name="filImgs"/>
@* <input type="hidden" name="ImgUrl" value="@Model.img"/>*@ </div>
<p>
<input type="submit" value="上传" />
</p> }

  

[HttpPost]
public ActionResult Create(UserInfo userinfo)
{
var file = Request.Files[0]; var img = //处理图片 userinfo.img = img; try
{
if (ModelState.IsValid)
{
db.userinfo.Add(userinfo);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
throw new Exception(); }
}
catch (Exception)
{ return View("Create");
} }

  

编辑

new { enctype = "multipart/form-data" } 这个必须要有
@using (Html.BeginForm(Html.BeginForm("edit", "UserInfo", FormMethod.Post, new { enctype = "multipart/form-data" })))
{ <div class="editor-label">
<span>图片</span>
</div>
<div class="editor-field">
<input type="file" name="filImgs"/>
@* <input type="hidden" name="ImgUrl" value="@Model.img"/>*@ </div>
<p>
<input type="submit" value="修改" />
</p> }
[HttpPost]
public ActionResult Edit(int id,UserInfo user)
{ var userinfo = db.userinfo.Find(id);
//ModelState.IsValid,此处相当于后台验证,防止前台验证因为各种情况被跳过或失效
try
{
bool updateRes = TryUpdateModel(userinfo);//如果字段符合则会赋值,否则保持原有 if (updateRes)
{
//图片
var file = Request.Files[];
var img =// 图片处理
if (img.Length>)
{
userinfo.img = img;
} db.SaveChanges();
return RedirectToAction("Index");
}
else
{ ModelState.AddModelError("", "更新失败!");
return View(userinfo);
} }
catch (Exception)
{ return View(userinfo);
} }

MVC4 图片上传的更多相关文章

  1. MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件

    场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...

  2. IOS5开发-http get/post调用mvc4 webapi互操作(图片上传)[转]

    IOS5开发-http get/post调用mvc4 webapi互操作(图片上传)   目前最流行的跨平台交互是采用http协议通过JSON对象进行互操作.这种方式最简单,也很高效.webservi ...

  3. 【HTML5+MVC4】xhEditor网页编辑器图片上传

    准备工作: 创建一个MVC项目中,添加好xhEditor插件 相关用法:http://www.cnblogs.com/xcsn/p/4701497.html 注意事项:xhEditor分为v1.1.1 ...

  4. 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片

    在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...

  5. Asp.Net Mvc 使用WebUploader 多图片上传

    来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...

  6. 06.LoT.UI 前后台通用框架分解系列之——浮夸的图片上传

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  7. JS图片上传预览插件制作(兼容到IE6)

    其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方 ...

  8. HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术

    最近做项目在前端我使用了很多新技术,这些技术有bootstrap.angularjs,不过最让我兴奋的还是使用了HTML5的技术,今天我想总结一些HTML5的技术,好记性不如烂笔头,写写文章可以很好的 ...

  9. 对百度的UEditor多图片上传的一些补充

    我已经写了一篇文章关于百度的UEditor提取多图片上传模块.如果还没有看过,请点击以下链接查看 http://www.cnblogs.com/luke1006/p/3719029.html 出差了两 ...

随机推荐

  1. IntervalZero RTX 2014

    2692407267@qq.com,很多其它内容请关注http://user.qzone.qq.com/2692407267 IntervalZero RTX 2014   上图 watermark/ ...

  2. 【M9】利用destructors避免泄漏资源

    1.在堆上获取的动态资源,用户忘记delete,或者由于异常导致没有没执行到delete,都会造成资源泄漏. 2.我们知道,栈上的对象,离开作用域,必定要执行析构方法.即使抛出异常,会堆栈回滚,保证已 ...

  3. 2013 US Open Award Ceremoney

    http://v.youku.com/v_show/id_XNjA3MjU3MzY4.html?firsttime=0 Singapore how about    another hand   tr ...

  4. [Angular2 Form] Create and Submit an Angular 2 Form using ngForm

    Forms in Angular 2 are essentially wrappers around inputs that group the input values together into ...

  5. [AngularJS] TweenList 3D + AngularJS Animate

    AngularJS animations and TweenLite make it really easy to create cool 3d effects in your application ...

  6. 大型JavaScript应用程序架构模式

    11月中旬在伦敦举行的jQuery Summit顶级大会上有个session讲的是大型JavaScript应用程序架构,看完PPT以后觉得甚是不错,于是整理一下发给大家共勉. PDF版的PPT下载地址 ...

  7. Control.Refresh Control.Invalidate 和 Control.OnPaint之间的联系和区别

    1.Control.Invalidate会放一个WM_PAINT消息到消息队列,当Control处理到该消息的时候,就调用OnPaint. 2.Control.Refresh相当于以下两行:Contr ...

  8. scala的下划线

    1.作为“通配符”,类似Java中的*.如import scala.math._ 2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = sum(1 to 5:_*)就 ...

  9. InActon-日志分析(KPI)

    我参照的前辈的文章http://blog.fens.me/hadoop-mapreduce-log-kpi/ 从1.x改到了2.x.虽然没什么大改.(说实话,视频没什么看的,看文章最好) 先用mave ...

  10. BootStrap2学习日记20---定制缩略图

    先看看效果: 代码: <ul class="thumbnails"> <li class="span3"> <div class= ...