jquery.form.js实现异步上传
前台页面
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script src="~/Scripts/jquery.form.js"></script>
<title>upload</title>
</head>
<body>
<form id="fileForm" method="post" enctype="multipart/form-data" action="/upload/upload">
文件名称:<input name="fileName" type="text"><br />
上传文件:<input name="myfile" type="file" multiple="multiple"><br />
<input id="submitBtn" type="submit" value="提交">
<img src="#" alt="my img" id="iceImg" width="300" height="300" style="display: block;" />
</form>
<input type="text" name="height" value="170" />
<input id="sbtn2" type="button" value="提交表单2">
<input type="text" name="userName" value="" />
<script type="text/javascript">
$(function () {
$("#fileForm").ajaxForm({
//定义返回JSON数据,还包括xml和script格式
//clearForm Boolean值属性,表示是否在表单提交成功后情况表单数据
//dataType 提交成果后返回的数据格式,可选值包括xml,json或者script
//target 服务器返回信息去更新的页面对象,可以是jquery选择器字符串或者jquer对象或者DOM对象。
//type 提交类型可以是”GET“或者”POST“
//url 表单提交的路径
dataType: 'json',
beforeSend: function () {
//表单提交前做表单验证
$("#myh1").show();
},
success: function (data) {
//提交成功后调用
//alert(data.message);
$("#iceImg").attr('src', '/upload/img/' + data.fileName);
$("#myh1").hide();
//防止重复提交的方法
//1.0 清空表单数据
$('#fileForm').clearForm();
//2.0 禁用提交按钮
//3.0 跳转页面
}
});
$("#myfile").change(function () {
$("#submitBtn").click();
});
$("#iceImg").click(function () {
$("#myfile").click();
});
});
</script>
<h1 id="myh1" style="display: none;">加载中...</h1>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace IceMvc.Controllers
{
public class UploadController : Controller
{
//
// GET: /Upload/ public ActionResult Index()
{
return View();
} [HttpPost]
public ActionResult Upload()
{
var filesList = Request.Files;
for (int i = ; i < filesList.Count; i++)
{
var file = filesList[i];
if (file.ContentLength > )
{
if (file.ContentLength > )
{
return Content("<script>alert('注册失败!因为您选择图片文件不能大于5M.');window.location='/User/Register';</script>");
} //得到原图的后缀
string extName = System.IO.Path.GetExtension(file.FileName);
//生成新的名称
string newName = Guid.NewGuid() + extName; string imgPath = Server.MapPath("/upload/img/") + newName; if (file.ContentType.Contains("image/"))
{
using (Image img = Image.FromStream(file.InputStream))
{
img.Save(imgPath);
}
var obj = new { fileName = newName };
return Json(obj);
}
else
{
//return Content("<script>alert('注册失败!因为您未选择图片文件.');window.location='/User/Register';</script>");
}
}
} return Content("");
} public ActionResult Afupload()
{
return View();
}
}
}
jquery.form.js实现异步上传的更多相关文章
- jquery.form.js 实现异步上传
前台: <form id="formSeacrh" action="/ResumeInfo/uploadFile" method="post&q ...
- jquery.form.js ajax提交上传文件
项目中最近有用到表单提交,是带有图片上传的表单录入,需要ajax异步提交,网上找了好多例子都是只能提交上传字段一个信息的,这里整理一下.表单里有普通文本信息字段也有图片上传字段. 1.jsp代码--引 ...
- [Asp.net mvc]jquery.form.js无刷新上传
写在前面 最近在自己的网盘项目中想用ajax.beginform的方式做无刷新的操作,提交表单什么的都可以,但针对文件上传,就是个鸡肋.在网上查找了发现很多人都遇到了这个问题,大部分都推荐使用jque ...
- asp.net使用jquery.form实现图片异步上传
首先我们需要做准备工作: jquery下载:http://files.cnblogs.com/tianguook/jquery1.8.rar jquery.form.js下载:http://files ...
- 解决 jquery.form.js和springMVC上传 MultipartFile取不到信息
前段页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- ThinkPHP+JQuery实现文件的异步上传
前端代码 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- jquery.form 兼容IE89文件上传
导入部分 <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset=&quo ...
- jQuery插件之ajaxFileUpload异步上传
介绍 AjaxFileUpload.js 是一个异步上传文件的jQuery插件,原理是创建隐藏的表单和iframe然后用JS去提交,获得返回值. 下载地址: http://files.cnblogs. ...
- PHP JS JQ 异步上传并立即显示图片
提交页面: <! DOCTYPE html> < html> < head> < meta charset ="GB2312" > ...
随机推荐
- IOS 推送-配置与代码编写
IOS 推送配置与代码编写 这里介绍IOS的推送,本文章已经在IOS6/7/8上都能运行OK,按照道理IOS9应该没问题. 大纲: 1.文章前提 2.推送介绍 3.推送文件账号设置 4.推送证书介绍 ...
- 关于redis启动流程介绍
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/94.html?1455870894 1. 准备运行环境 * 设置oom h ...
- lua跨平台文件夹遍历匹配查找
require"lfs" --[[Desc:在B路径D文件中下 搜寻A路径下的没用到的C类文件: 并且将没用到的B类文件名称打印出来: 设置好路径拖到lua自带编辑器中即可运行之; ...
- Android WebView 总结 —— 使用HTML5播放视频及全屏方案
在APP开发的过程中,会碰到需要在WebView中播放视频的需求,下面讲解一下如何在WebView中使用html5播放视频. 1.让视频在各个Android版本能够正常播放 在AndroidManif ...
- Android ListView 进阶学习
1.使用ListView展示数据结构为二维数组的数据 当我们遇到数据结构是二维数组的需求的时候,我们会首先想到ListView,但是要想实现二维数组,会想到ListView里面嵌套ListView,但 ...
- 学习ASP.NET MVC(八)——“Code First Migrations ”工具
在本篇文章中,我们学习如何使用实体框架的“Code First Migrations ”(也称为代码先行功能)工具,使用其中的“迁移”功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 默认情 ...
- VUX 移动前端框架使用文档
VUX 移动前端框架使用文档 https://owlaford.gitbooks.io/vux-mobile-framework/content/index.html
- 开源IM工程“蘑菇街TeamTalk”的现状:一场有始无终的开源秀
1.前言 随着云IM的发展,已吸引越来越多有IM需求的APP接入.但考虑到云IM无论从商业模式还是运营模式上,还需经过多年的沉淀,才可能真正实现客户与服务商的运营和服务良性循环的双赢局面.在此之前,加 ...
- Liferay7 BPM门户开发之40: Form表单的Action到Render的数据传递
在Form提交后的变量,很多情况是要展现在jsp页面中,这时Action到Render的变量传递就非常有用. 例如,您在数据库中添加了学生的详细信息. 为了实现这一需求,先创建Form表单(学生的细节 ...
- Myeclipse 操作数据库
步骤1:通过MyEclipse中的window->show View->other 调出.DB浏览器,和 SQL Results 步骤2. 可以右键单击空白处,选择new,创建一个新的DB ...