注意事项:

1.提交form,必须引用jquery.form.min.js

2.不要使用mvc自带的Ajax.Form()

1.页面cshtml

<form name="frmInput" id="frmInput" method="post" action="@Url.Action(ViewContext.RouteData.Values["Action"].ToString())" enctype="multipart/form-data" >
<input id="f1" name="f1" type="file">
<input type="button" value="保存" onclick="Input.Save(this)" class="btn-8" />
</from> Input.Save = function (e) {
$('#frmInput').ajaxSubmit({
url: Url,
error: function (request) {
alert('保存出错,请重试!');
},
success: function (data) {
var dataObj = eval("(" + data + ")");//转换为json对象
         //方法二
         //var dataObj=jQuery.parseJSON(data);
if (dataObj.IsOK) {
//刷新列表
alert("保存成功!");
}
else {
alert('保存失败!');
}
}
});
}

2.后台

示例1,忘记什么时候写的,以后看到再修改补充

[HttpPost]
public JsonResult Create(MerchantsModel model, FormCollection form)
{
return new JsonResult() { ContentType = "text/html", Data = object };// return Json(result, "text/html", Encoding.UTF8);
}

示例2

[HttpPost]
public ActionResult void Create(Model modelName, FormCollection form)
{
var requestFiles = Request.Files;//HttpFileCollectionBase
if (requestFiles.Count > )
{
for (int i = ; i < requestFiles.Count; i++)
{
//此块代码仅作示例
//文件名称 requestFiles[i].FileName
var postedfile = requestFiles[i];//HttpPostedFileBase
var savePath="d://d.jpg";
postedfile.SaveAs(savePath);
}
}
   return Json(result, "text/html", Encoding.UTF8);
}

3. 解决问题

返回<pre style="word-wrap: break-word; white-space: pre-wrap;">....</pre> 的问题

4. HttpPostedFile 转为HttpPostedFileBase

它们之间是两个独立的东西,需要通过HttpPostedFileWrapper转换,犹如 HttpContext和HttpContextBase要通过HttpContextWrapper 包装,是.NET3.5时才有的,使用.NET2.0类库可能会遇到,这里记录下。

 public bool Upload(HttpPostedFile file)
{
HttpPostedFileBase hpfb = new HttpPostedFileWrapper(file);
return false;
}

来自:http://www.cnblogs.com/Kummy/archive/2013/02/27/2934608.html

推荐写法:Application_Start中统一检测

 private void CheckUploadDirectory()
{
string assemblyDirectory = AppDomain.CurrentDomain.BaseDirectory;
assemblyDirectory = Path.Combine(assemblyDirectory, "Upload");
if (!Directory.Exists(Path.Combine(assemblyDirectory, "Logo")))
{
Directory.CreateDirectory(Path.Combine(assemblyDirectory, "Logo"));
}
if (!Directory.Exists(Path.Combine(assemblyDirectory, "Problems")))
{
Directory.CreateDirectory(Path.Combine(assemblyDirectory, "Problems"));
}
}

MVC ajaxSubmit上传图片的更多相关文章

  1. spring mvc 的上传图片是怎么实现的?

    spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...

  2. mvc实现上传图片(上传和预览)webuploader

    笔者看到mvc最近比较流行,而很多使用一些比较旧的的方法上传图片,再次安利一下百度的webuploader控件吧 webuploader第一步要先下载一些插件这点可以在webuploader官网上下载 ...

  3. ASP.NET MVC 4 - 上传图片到数据库

    这里演示如何在MVC WEB应用程序如何上传图片到数据库以及如何在WEB页面上显示图片.数据库表对应整个Model类,不单图片数据一个字段,我们从数据表的定义开始: CREATE TABLE [dbo ...

  4. MVC异步上传图片到本地/服务器

    这两天朋友问我,有没有异步上传图片到本地/服务器这种demo,他有用, 我就想,好吧, 那刚好周末了,整理一套出来. 主要用到的是jquery uploadify 这个juqery的插件 ,可以无刷新 ...

  5. jquery.uploadify+spring mvc实现上传图片

    一.前端页面 1.下载jquery.uploadify 去uploadify官网(http://www.uploadify.com/download/)下载压缩包,解压后放在如下路径: 2.html结 ...

  6. MVC实现上传图片的方法

    Form提交时,须注意form需要添加属性enctype="multipart/form-data",否则Request.Files.Count=0,无法上传图片. cshtml代 ...

  7. spring mvc做上传图片,文件小于10k就不生成临时文件了

    这是spring-mvc.xml中的 <bean id="multipartResolver" class="org.springframework.web.mul ...

  8. .NET MVC Dropzone 上传图片

    在nuget控制台输入:Install-Package dropzone @{ Layout = null; } <!DOCTYPE html> <html> <head ...

  9. mvc中上传图片到指定文件夹中

    前台: @using (Html.BeginForm("AddImg", "UpFileImg", FormMethod.Post, new { enctype ...

随机推荐

  1. SharePoint 2013 单一页面赋设计权限

    本文介绍SharePoint的使用中,断开单一页面权限,给用户编辑权限以及操作中遇到的问题,希望给相关需要的人一个参考. 1.首先进入页面库,找到我们的页面,进入共享,如下图: 2.在弹出的窗口中选择 ...

  2. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q127-Q130)

    Question 127You create a custom list named Products.You need to perform a Representational State Tra ...

  3. MAC中安卓开发环境的下载(转)

    今天终于为我的Macbook Pro Retina搭建好了Android开发环境,几经折磨,差点放弃了: 总结如下:1.最好选择ADT Bundle,这里面已经集成好了Eclipse.ADT.Andr ...

  4. Service的生命周期&Service绑定方法

    服务的生命周期: 一.采用start的方式开启服务 生命周期如下: 开启服务: onCreate()--> onStartCommand() (onStart()过时了)---> onDe ...

  5. android 开发小记

    1.fragment & activity when config changes, activity will restart but fragment can be store. 2.te ...

  6. android中的坐标系以及获取坐标的方法

    android中有两种坐标系,分别称之为Android坐标系和视图坐标系.而对应的也有一些相关的方法可以获取坐标系中的 坐标值.只有搞清楚这些区别,才能在实现的时候不至于出错或者得不到你想要的效果. ...

  7. Effective Java 02 Consider a builder when faced with many constructor parameters

    Advantage It simulates named optional parameters which is easily used to client API. Detect the inva ...

  8. C#中Dictionary,Hashtable,List的比较及分析

    一. Dictionary与Hashtable Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值 二者的特性大体上是相同的,有时可以把Dicti ...

  9. TOMCAT报错:HTTP Status 404 -

    构建struts2工程师,tomcat报错: HTTP Status 404 - type Status report message description The requested resour ...

  10. 窗体DataGridView控件中按回车键时,单元格向下移动,如何能改成向右移动

    方法一:protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e) { base.OnKeyUp(e); if (e.Ke ...