mvc 上传文件

index.aspx

 <div>
        <% using (Html.BeginForm("Index", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
           {%>
        <input type="file" name="file" />
        <input type="submit" name="upload" value="提交" />
        <%} %>
    </div>

Controllers文件夹 - FileController.cs

 [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            if (file != null)
            {
                //获取文件路径
                string strpath = Server.MapPath("~/files/"
                    + file.FileName.Substring(file.FileName.LastIndexOf(@"\")));
                file.SaveAs(strpath);//把图片保存在此路径中
                return Content("<script>alert('上传文件成功!');location.href='" + Url.Content("~/File") + "'</script>");
            }
            return View("Index");
        }

posted @ 2020-10-05 21:34  小九家的丫头  阅读(128)  评论(0编辑  收藏  举报