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");
        }

mvc 上传文件

上一篇:A Bipartite Network Module-Based Project to Predict Pathogen–Host Association


下一篇:HTTP三次握手