Asp.net mvc4用iframe实现异步上传

发布时间:2020-04-07 15:57:05 作者:桂素伟
来源:网络 阅读:1626

1.         Model层用一个TestModel(与上一篇博文的TestModel 相同)

2.         Controller层:
 public class TestController : Controller
{
//这是iframe页面的action
 public ActionResult IframeView()
        {
            return View();
        }
        public ActionResult View2()
        {
            return View();
        }
        /// <summary>
        /// 提交方法
        /// </summary>
        /// <param name="tm">模型数据</param>
        /// <param name="file">上传的文件对象,此处的参数名称要与View中的上传标签名称相同</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult View2(TestModel tm, HttpPostedFileBase file)
        {
            if (file == null)
            {
                return Content("没有文件!", "text/plain");
            }
            var fileName = Path.Combine(Request.MapPath("~/UploadFiles"), Path.GetFileName(file.FileName));
            try
            {
                file.SaveAs(fileName);
                tm.AttachmentPath = fileName;//得到全部model信息
                return Content("上传成功!", "text/plain");
            }
            catch
            {
                return Content("上传异常 !", "text/plain");
            }
        }
}
View层,现在有两个View层,一个是iframe,叫IframeView,一个是View2,分别如下:
IframeView
@model UploadFile.Models.TestModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IframeView</title>
</head>
<body>
    @*enctype= "multipart/form-data"是必需有的,否则action接收不到相应的file,这里报交的action是View2*@
    @using (Html.BeginForm("View2", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.LabelFor(mod => mod.Title)
        <br />
        @Html.EditorFor(mod => mod.Title)
        <br />     <br />
        @Html.LabelFor(mod => mod.Content)
        <br />
        @Html.EditorFor(mod => mod.Content) 
        <br />   
        <span>上传文件</span>
        <br />
        <input type="file" name="file" />
        <br />
        <br />
        <input id="ButtonUpload" type="submit" value="提交" />
    }
</body>
</html>
别一个是View2
@{
    Layout = null;
}
 
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>iframe异步上传</title>
</head>
<body>
     <form>
        <iframe width="500" height="500" src="/Test/IframeView"></iframe>
    </form>
</body>

</html>

推荐阅读:
  1. ASP.NET网址
  2. ASP.NET MVC4 捆绑(Bundle)技术下的 JavaScript

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

iframe public 异步上传

上一篇:Rancher:2016的答卷

下一篇:常见的缓存策略的优劣势对比

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》