栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MVC中的Ajax.BeginForm上传文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVC中的Ajax.BeginForm上传文件

更好的方法是使用jquery表单插件。

这是示例:

Html.BeginForm

 @using (Html.BeginForm("YourAction", "YourController")){    @Html.AntiForgeryToken()    <input type="file" name="files"><br>    <input type="submit" value="Upload File to Server">}

行动方法

    [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 > 0)     {         // 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);     } }        }    }

进度条

<div >   <div >0%</div></div>

jQuery和表单脚本

<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,只需将其转换为Html.BeginForm()。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号