【转】利用Ajax.BeginForm提交文件
Ajax.BeginForm
@using (Ajax.BeginForm("YourAction", "YourController", new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
<input type="file" name="files"><br>
<input type="submit" value="Upload File to Server">
}
Action Method
[HttpPost]
[ValidateAntiForgeryToken]
public void YourAction(IEnumerable<HttpPostedFileBase> files)
{
if (files != null)
{
foreach (var file in files)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > )
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// TODO: need to define destination
var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
file.SaveAs(path);
}
}
}
}
【引者注.有时加入 [ValidateAntiForgeryToken]注解会出错,具体原因不明,可删除这行注释】
Progress Bar
<div class="progress progress-striped">
<div class="progress-bar progress-bar-success">0%</div>
</div> <div id="status"></div>
Jquery & Form script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script> <script>
(function() { var bar = $('.progress-bar');
var percent = $('.progress-bar');
var status = $('#status'); $('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
success: function() {
var percentVal = '100%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
}); })();
</script>
【转】利用Ajax.BeginForm提交文件的更多相关文章
- MVC Ajax.BeginForm 提交上传图片
吃水不忘挖井人,如果对你有帮助,请说声谢谢.如果你要转载,请注明出处.谢谢! 异步提交时,出现图片不能上传. 起初我定格在 System.Web.Mvc 中.查询源码时,也是没有问题的.那问题出现 ...
- Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证
1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...
- Asp.net Mvc Ajax.BeginForm提交表单
之前Mvc中一直用Html.BeginForm提交表单,即如下: @using (Html.BeginForm("Add", "News", FormMetho ...
- 利用ajax.dll类库文件实现无刷新
使用这种方法前需要配置相应的环境 1.引用ajax.dll文件 2.在web.config添加如下: <httpHandlers> <add path="ajax/*. ...
- SpringMVC,SpringBoot利用ajax上传文件到后台
1.传递单文件 首先html文件中有个<input type=”file” name=”file” id=”file”/>元素. 前台js写法: var formData=new Form ...
- ajax异步提交文件
首先 下载jquery和jquery.form.js http://malsup.com/jquery/form/ <script type="text/javascript&qu ...
- ajax方式提交文件到后台同时加其他参数
struts2后台Action方法,直接用参数成员变量对象的属性接收即可
- MVC Ajax.BeginForm重复提交解决方法
mvc使用MVC Ajax.BeginForm提交的时候有重复提交结果的时候检查相关js文件引用情况, 其中mvc4注意 1 2 3 4 @Scripts.Render("~/bundles ...
- ajax上传文件显示进度
下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...
随机推荐
- Java之工厂模式
interface Fruit { void eat(); } class Apple implements Fruit { public void eat() { S ...
- 类 BufferedReader
以前学习的时候也没有太在意,在项目中使用到了才发现呵呵 1.读取一个txt文件,方法很多种我使用了字符流来读取(为了方便) FileReader fr = new FileReader("f ...
- 手机端 UI一些插件
手机弹出框 http://yun.baidu.com/share/link?shareid=3523128425&uk=2685891615
- 解决Thinkpad E450 外接显示器后没有声音的问题
昨天把公司配的ThinkPad E450 带回来了,今天用的时候一切正常没什么问题. 因为要看边看视频边做点别的,我就外接一台显示器,说出来我的这台显示器,你们绝对想不到是什么 显示器.我的这台显示器 ...
- codeforces 149E . Martian Strings kmp
题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...
- python学习【一】基础入门
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- software testing homework2
一.Checkstyle安装及使用 1.checkstyle插件包:http://sourceforge.net/projects/eclipse-cs/ checkstyle.xml配置文件:htt ...
- brief introduction JAVA new I/O (NIO)
Reference document: Getting started with new I/O (NIO) Preface: NIO was introduced with JDK1.4 for h ...
- (Problem 19)Counting Sundays
You are given the following information, but you may prefer to do some research for yourself. 1 Jan ...
- 安装CAD出现Error 1904.Module的解决方法
在安装AutoCAD2008时,安装过程中出现了一个小错误,虽然说不影响使用,也不影响功能,但还是需要把这个问题解决,今天就和大家分享解决这个问题的方法. 错误描述 会在安装过程中出现错误提示:Err ...