MVC文件上传

简单的文件上传

1.View (文件上传用的是 multipart/form-data 格式)

<div>

    @using (Html.BeginForm("SmallUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <input type="file" name="file"/>
    <input type="submit" value="上传" />
    }
</div>

  

 

2.Controller

 

 [HttpPost]
        public ActionResult SmallUpload(HttpPostedFileBase file)
        {
            try
            {
                if (file == null)
                {
                    return Content("没有文件!", "text/plain");
                }
                var pathForSave = Request.MapPath("~/Upload");
                if (!Directory.Exists(pathForSave))
                    Directory.CreateDirectory(pathForSave);
                var fileName = Path.Combine(pathForSave, Path.GetFileName(file.FileName));
                file.SaveAs(fileName);
                return Content("上传成功!", "text/plain");
            }
            catch (Exception ex)
            {
                return Content($"上传异常 {ex.Message} !", "text/plain");
            }
        }

 

断点续传:待定

 

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