废话不多说,上代码:

用到的js文件:

jquery.min.js

jquery.easyui.min.js

<input id="fileurl" onclick="$('#imageUpLoad').click();"/>
<input type="button" value="上传" onclick="document.getElementById('form1').submit();"> <div style="display:none; position:absolute; top:0; left:0;">
<iframe name="uploadResponse"></iframe>
<form id="form1" runat="server" action="/UploadFile/UpLoadImage/" method="post" enctype="multipart/form-data" target="uploadResponse">
<input type="file" id="imageUpLoad" name="imageUpLoad" style="display:none;" onchange="$('#fileurl').attr('isLoad','0');">
<input id="imageType" name="imageType" value="floor" style="display:none;"/>
</form>
<script type="text/javascript">
function CallBack(path) {
$("#fileurl").attr('isLoad', '1').val(path);
$.messager.alert('提示信息', '上传成功!', 'info');
}
</script>
</div>

isLoad 属性是我自己添加的属性,用来判断图片是否提交

ps:记得在window.load事件中初始化这个属性,因为低版本的ie不支持<input isLoad="1"/>这种格式

后台代码

using System.Web;
using System.Web.Mvc;public class UploadFileController : Controller
{
//
// GET: /UploadFile/ public ActionResult Index()
{
return View();
} public void UpLoadImage(HttpPostedFileBase imageUpLoad, string imageType)
{
string returnFilePath = imageUpLoad.FileName;
//这里是我自己写的一段创建文件路径并保存的代码,可以直接imageUpLoad.SaveAs("文件路径");来保存
Helper.SaveFile(imageUpLoad, Helper.FileType.Images, imageType, ref returnFilePath); returnFilePath = returnFilePath.Replace("\\", "/"); Response.Write(string.Format("<script type='text/javascript'>window.parent.CallBack('" + returnFilePath + "');</script>"));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO; public class Helper
{  

    /// <summary>
    /// 文件类型
    /// </summary>
    public enum FileType {
      Images,
      Files
    }

    public static bool SaveFile(HttpPostedFileBase f, FileType ft, string separator,ref string path)
{
try
{
string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ft.ToString());
if (!Directory.Exists(FilePath))
Directory.CreateDirectory(FilePath); FilePath = Path.Combine(FilePath, separator);
if (!Directory.Exists(FilePath))
Directory.CreateDirectory(FilePath); FilePath = Path.Combine(FilePath, path);
f.SaveAs(FilePath);
path = Path.Combine(ft.ToString(), separator, path);
return true;
}
catch (Exception ex){
LogHelper.Error(ex);
}
return false;
}
}

这里主要说明一下html界面form的参数:

action : 你的mvc处理文件上传的路径

method:提交方式

enctype:这个一定要写对“multipart/form-data”

target:值“uploadResponse”表示回传的接收页面地址,这里是一个iframe,避免了回传刷新页面

后台cs代码说明:

UpLoadImage:方法名要和地址一致
参数:
HttpPostedFileBase imageUpLoad  
imageUpLoad 应该是上传文件控件的name值
string imageType

同上iamgeType 为文本控件的name值
如果你有多个参数需要传递到后台,可以按照这个格式自定义参数个数 当然还有一种方法可以放函数不带参数
使用request来处理传递过来的文件和参数,请网上自行查阅资料。

MVC ajax 上传文件的更多相关文章

  1. asp.net MVC ajax上传文件

    普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...

  2. C# MVC ajax上传 文件

    用普通的ajax提交表单的时候,不能把文件流传到后端去,所以要用到jquery.form.js jquery.form.js到官网下载或者从这里下载:http://pan.baidu.com/s/1c ...

  3. spring mvc + ajax上传文件,页面局部刷新

    1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取, 因为包含文件的上传,所以采用FormData的形式来进行数据交互,通过append将 ...

  4. MVC Ajax上传文件

    前台:首先要导入jQuery <html><head> <meta name="viewport" content="width=devic ...

  5. .net mvc ajax 上传文件

    1.前端 <div> <input type="file" id="upfile" /> <button type="b ...

  6. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  7. Asp.Net Mvc异步上传文件的方式

    今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...

  8. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  9. 关于Extjs MVC模式上传文件的简单方式

    Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...

随机推荐

  1. Amarino例程无法使用的问题

    Serial.begin(9600); 而不是用它的57600

  2. linux 下按在sqllite

    1 安装 去sqlite主页http://www.sqlite.org/.跳转到下载也http://www.sqlite.org/download.html.源码下载sqlite-amalgamati ...

  3. 通过DDOS攻击流程图来浅谈如何预防Ddos攻击与防御

    DDOS攻击流程图 站长之家配图(来源:ppkj.net) 一 背景 在前几天,我们运营的某网站遭受了一次ddos攻击,我们的网站是一个公益性质的网站,为各个厂商和白帽子之间搭建一个平台以传递安全问题 ...

  4. Golang做的验证码(2)

    前面一篇文章介绍了2个用Golang做的验证码 http://www.cnblogs.com/ghj1976/p/3392847.html  这里再补充几个: 1.在GAE上使用的Google的验证码 ...

  5. LR之脚本调试

    1.概述 2.Animated run和Non-animated run 3.调试小技巧 4.日志设置

  6. Selenium2Library系列 keywords 之 _SelectElementKeywords

    # 公有方法 (1)get_list_items(self, locator)  返回labels集合 _get_select_list_options(self, select_list_or_lo ...

  7. php 在线 mysql 大数据导入程序

    1 <?php header("content-type:text/html;charset=utf-8"); error_reporting(E_ALL); set_tim ...

  8. 面试体验:Facebook 篇(转)

    http://www.cnblogs.com/cathsfz/archive/2012/11/05/facebook-interview-experience.html 2012-11-05 08:2 ...

  9. bzoj 3289 Mato的文件管理(莫队算法+BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题意] 回答若干个询问:[l,r]区间内的逆序对个数. [思路] 莫队算法,B ...

  10. 阿里云存储OSS之九大使用技巧

    http://www.biphp.com/cloud-computing/%E9%98%BF%E9%87%8C%E4%BA%91%E5%AD%98%E5%82%A8oss%E4%B9%8B%E4%B9 ...