NET MVC 上传文件

1.HTML

@using (Html.BeginForm("UploadFile", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    
"margin:13px;padding:13px;"> "file" style="float:left;" name="myFile" />
"submit" value="提交" /> }

"myfrom" id="myform" method="post" action="~/Student/UploadFile">
"margin:13px;padding:13px;"> "file" style="float:left;" name="myFile" />
"submit" value="提交" />

2.Script:手动submit

3.UploadFileAction:Import是导入视图

        /// 
        /// 页面添加一个“导入数据”读取将“文件导入.xlsx”里面的学生信息,保存至“学生.xml”文件中
        /// 
        /// 上传文件结果信息
        [HttpPost]
        public ActionResult UploadFile()
        {
            HttpPostedFileBase file = Request.Files["myFile"];
            if (file != null)
            {
                try
                {
                    //  file.FileName//文件名
                    //  file.InputStream//文件流
                    TempData["message"] = "导入成功!";
                    return View("Import");
                }
                catch (Exception ex)
                {
                    //return Content(string.Format("上传文件出现异常:{0}", ex.Message));
                    TempData["message"] = string.Format("上传文件出现异常:{0}", ex.Message);
                    return View("Import");
                }

            }
            else
            {
                return View("Import");
            }
        }

 

你可能感兴趣的:(NET MVC 上传文件)