注意事项:

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. 一个帖子学会Android开发四大组件

    来自:http://www.cnblogs.com/pepcod/archive/2013/02/11/2937403.html 这个文章主要是讲Android开发的四大组件,本文主要分为 一.Act ...

  2. 网站防止CC攻击的方法

    CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见的网站攻击方法,攻击者通过代理服务器或者肉鸡向向受害主机不停地发大量数据包,造成对方服务器资源耗尽,一 ...

  3. How to copy files between sites using JavaScript REST in Office365 / SharePoint 2013

    http://techmikael.blogspot.in/2013/07/how-to-copy-files-between-sites-using.html I'm currently playi ...

  4. iOS多线程技术

    iOS多线程技术主要分配NSThread.NSOperation和GCD.下边来简单的介绍一下吧. 随性一点,就不按照顺序来了.所以先介绍一下NSOperation. ---------------- ...

  5. 通过注册的URL Scheme向目标APP传递参数

    通过注册的URL Scheme向目标APP传递参数 通过URL Scheme启动APP很简单就可以做到,但有时候我们想在启动APP的时候传递一些参数,这个时候我们就可以通过URL Scheme自定义U ...

  6. iOS开发之网络数据解析--GDataXML解析框架的使用

      前言:GDataXML是google基于C语言libxml2写的第三方框架,该源码文件就一个类,看其源码,基本使用了C语言的底层的很多lib编译库代码,所以刚导入使用,会报错提示需要设置导入需要的 ...

  7. Finder增强插件XtraFinder

    关于在Mac上安装XtraFinder插件,现在因为Mac更新到10.11, Mac OS X 10.11(El Capitan)默认开启了 SIP(System Integrity Protecti ...

  8. 最新Burpsuite Pro v1.7.03 介绍和破解版下载

    0x00 介绍 Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP 消息, ...

  9. andorid开发eclipse常见问题

    1.报错: BUILD FAILED D:\workspace\ganji\build.xml:144: The following error occurred while executing th ...

  10. 阿里云ECS/Ubuntu下JDK、Tomcat、MySQL安装记录

    今天六一儿童节,然后... ... ... ... 然后就是父亲节呀孩子们!!! ———————————————————————割———————————————————————— 同事需要JDK.To ...