首页 > Web开发 > 详细

ASP.NET MVC- Upload File的例子

时间:2015-11-23 16:43:34      阅读:126      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!