MVC ajax 上传文件
废话不多说,上代码:
用到的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 上传文件的更多相关文章
- asp.net MVC ajax上传文件
普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...
- C# MVC ajax上传 文件
用普通的ajax提交表单的时候,不能把文件流传到后端去,所以要用到jquery.form.js jquery.form.js到官网下载或者从这里下载:http://pan.baidu.com/s/1c ...
- spring mvc + ajax上传文件,页面局部刷新
1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取, 因为包含文件的上传,所以采用FormData的形式来进行数据交互,通过append将 ...
- MVC Ajax上传文件
前台:首先要导入jQuery <html><head> <meta name="viewport" content="width=devic ...
- .net mvc ajax 上传文件
1.前端 <div> <input type="file" id="upfile" /> <button type="b ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- Asp.Net Mvc异步上传文件的方式
今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- 关于Extjs MVC模式上传文件的简单方式
Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...
随机推荐
- linux系统上Mysql数据库导入导出操作
需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作 ...
- memcache保存php的session 解决多服务器的session问题
PECL :: Package :: memcache 2.1.1 版本的 Changelog 中有一条: - Added experimental session storage support. ...
- 【LeetCode】100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【转】Bellman_ford算法
原文链接:http://www.cnblogs.com/Jason-Damon/archive/2012/04/21/2460850.html 摘自百度百科 Bellman-ford算法是求含负权图的 ...
- C#轻量级企业事务 - TransactionScope
using System; using System.Data.SqlClient; using System.Transactions; namespace SomeDBTransaction { ...
- IE对toLocaleString小数位处理
在js中对数值的格式化经常会用到四舍五入.保留小数位数.百分制格式化,分别会用到以下方法 <script type="text/javascript"> var n = ...
- 硬盘类型和Linux的分区
目前硬盘主要分为以下几种:IDE,SCSI,SATA,SAS.其中SAS(Serial Attached SCSI)即串行连接SCSI,属于SCSI的新一代技术. 以下是Linux常见设备的名称: 装 ...
- Linux文件系统 (Ubunt)
Linux 文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基本原理.文件存储 ...
- html5 canvas 钟表
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- onAttachedToWindow()在整个Activity生命周期的位置及使用
onAttachedToWindow在整个Activity的生命周期中占据什么位置? 为什么要在onAttachedToWindow中修改窗口尺寸? 一.onAttachedToWindow在Acti ...