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提交文件的更多相关文章

  1. MVC Ajax.BeginForm 提交上传图片

    吃水不忘挖井人,如果对你有帮助,请说声谢谢.如果你要转载,请注明出处.谢谢! 异步提交时,出现图片不能上传. 起初我定格在  System.Web.Mvc  中.查询源码时,也是没有问题的.那问题出现 ...

  2. Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证

    1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...

  3. Asp.net Mvc Ajax.BeginForm提交表单

    之前Mvc中一直用Html.BeginForm提交表单,即如下: @using (Html.BeginForm("Add", "News", FormMetho ...

  4. 利用ajax.dll类库文件实现无刷新

    使用这种方法前需要配置相应的环境 1.引用ajax.dll文件 2.在web.config添加如下: <httpHandlers>   <add path="ajax/*. ...

  5. SpringMVC,SpringBoot利用ajax上传文件到后台

    1.传递单文件 首先html文件中有个<input type=”file” name=”file” id=”file”/>元素. 前台js写法: var formData=new Form ...

  6. ajax异步提交文件

    首先 下载jquery和jquery.form.js   http://malsup.com/jquery/form/ <script type="text/javascript&qu ...

  7. ajax方式提交文件到后台同时加其他参数

    struts2后台Action方法,直接用参数成员变量对象的属性接收即可

  8. MVC Ajax.BeginForm重复提交解决方法

    mvc使用MVC Ajax.BeginForm提交的时候有重复提交结果的时候检查相关js文件引用情况, 其中mvc4注意 1 2 3 4 @Scripts.Render("~/bundles ...

  9. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

随机推荐

  1. MyEclipse 免安装版制作

    前言:以MyEclipse6.0为例,安装目录,例如如d:\java\MyEclipse6.0 (1)新建MyEclipse 6.0.bat文件 新建位置:当前MyEclipse根目录 文件内容: s ...

  2. 如何将mongodb bson文件转成json文件

    使用mongodb自带的命令 bsondump collection.bson > collection.json

  3. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  4. jQuery入门第二

    element选择器​ 在文具盒中,有铅笔.钢笔和水彩笔,类似于页面中的<div>.<span>各个元素,虽然同属于一个容器,但有各自的功能,jQuery中可以根据元素名查找元 ...

  5. JAVA排序(一) Comparable接口

    昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...

  6. Vim 实用技术,第 2 部分: 常用插件(转)

    http://blog.jobbole.com/20619/ 2.1. gzip(压缩文件支持) 作者:Bram Moolenar 网站脚本编号:无(包含在 Vim 的标准发布之中) 安装说明:无 功 ...

  7. cocos2dx CCControlSwitch

    CCControlSwitch也是extension中的控件,本身比较简单,直接上例子 // on "init" you need to initialize your insta ...

  8. BZOJ 无数据题集合

    题目 http://www.lydsy.com/JudgeOnline/problem.php?id=1142 http://www.lydsy.com/JudgeOnline/problem.php ...

  9. Static block start new thread

    Static block start new thread public class StaticThreadInit { static{ Threadt = newThread(){ public ...

  10. 第七届河南省赛10403: D.山区修路(dp)

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...