码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC- Upload File的例子

时间:2015-11-23 16:45:48      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

  上传文件是一项基本功能,一定要了解的。先来看一下使用ASP.NET MVC实现简单的上传。

一、简单的例子

  Controller的例子

        public ActionResult UploadDemo()
        {
            return View();
        }


        public ActionResult FileUpload()
        {
            HttpPostedFileBase file = Request.Files["file"];
            if (file != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);

                Response.Write("<script>alert(‘上传成功‘);location.href=‘/Index/UploadDemo‘ </script>");
            }
            else
            {
                Response.Write("<script>alert(‘上传失败‘);location.href=‘/Index/UploadDemo‘ </script>");
            }

            return null;
        }

  对应的View视图

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>UploadDemo</title>
</head>
<body>
    <div>
        <h2>
            上传例子</h2>
        @using (Html.BeginForm("FileUpload", "Index", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <input type="file" name="file" />
            <input type="submit" value="uploadFile" />
        }
    </div>
</body>
</html>

 

ASP.NET MVC- Upload File的例子

标签:

原文地址:http://www.cnblogs.com/cxeye/p/4988604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!