注意事项:

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. Windows Azure 上传 VM

    One of the great features of Windows Azure is VHD mobility. Simply put it means you can upload and d ...

  2. SharePoint 2010 文档管理系列之准备篇

    前言:很早自己就想写一个系列的文章,但是不知道写什么,最近在QQ群里,好多人说在做文档管理,其实文档管理也是SharePoint的一个很不错的功能点,自己想了想,也想多学习点东西,所以写这个主题吧,今 ...

  3. Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"

    Article ID: 939308 - View products that this article applies to. Expand all | Collapse all Symptoms ...

  4. 关于android4.3 Intel X86 Atom System Image的下载

    今天建立android4.3模拟器的时候发现没有android4.3 Intel X86 Atom System Image可选,打开android SDK Manager 于是希望重现选择下载安装, ...

  5. 字符串匹配--Karp-Rabin算法

    主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...

  6. 【读书笔记】iOS网络-HTTP-请求内容

    一,GET方法. 从服务器获取一段内容,用HTTP术语来说就是实体.GET请求通常不包含请求体,不过也是可以包含的.有些网络缓存设施只会缓存GET响应.GET请求通常不会导致服务器端的数据变化. 二, ...

  7. Spring(二)Bean入门

    一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...

  8. XtraScheduler 日程控件显示自定义标题

    下面代码实现一个自定义日程标题 public class CustomHeaderCaptionService : HeaderCaptionServiceWrapper { public Custo ...

  9. 第八篇 :微信公众平台开发实战Java版之如何网页授权获取用户基本信息

    第一部分:微信授权获取基本信息的介绍 我们首先来看看官方的文档怎么说: 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域 ...

  10. 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能

    1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...