注意事项:

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. centOS 安装 svnserve

    安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 ? 1 2 3 4 5 6 7 8 9 1 ...

  3. Android WebView 获取网页的标题

    final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); final WebView webView = (WebView)f ...

  4. swift基础一

    // swift中导入类库使用import,不再使用<>和"" import Foundation // 输出 print("Hello, World!&qu ...

  5. Struts2(十六)Json

    一.JSON Json就是浏览器和服务器之间交换数据的一种轻量级对象 javaSctipt中类似的对象操作 $(function() { var person = { "name" ...

  6. spring boot 1.4.1 with jsp file sample

    <!--pom.xml--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  7. 解决Junit单元测试 找不到类 ----指定Java Build Path

    做junit 单元测试时,发现怎么执行都是以前编译过得代码. 最后找到原因了, src/test/java 编译完的.class路径是 Default output folder Default ou ...

  8. javascript 定时器

    setTimeout()--用于指定在一段特定的时间后执行某段程序.           格式: [定时器对象名=]setTimeout("<表达式>",毫秒数); 功 ...

  9. 深入理解java虚拟机(1)------内存区域与内存溢出

    在C++领域,关于C++的内存存储,结构等等,有一本书:深度探索C++对象模型,讲解的非常透彻. 而Java确把这一工作交给了虚拟机来处理. 我们首先来看看关于内存的问题. 1.问题: 1)java ...

  10. JavaScript Patterns 4.7 Init-Time Branching

    When you know that a certain condition will not change throughout the life of the program, it makes ...