显示页面内容

@*new { enctype = "multipart/form-data" }比不可少,否则上传文件不会成功 *@
@using (Html.BeginForm("MaterielImport", "Sys", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<text>选择上传文件:</text><input name="file" type="file" id="file" />

<input type="submit" name="Upload" value="导 入" class="bt_bg" />

<input type="button" value="清空原有信息" onclick="DeleInformation()" />
}

 

控制器代码

public ActionResult MaterielImport(FormCollection form)
{
string size = Request.Files[0].ContentLength.ToString();//文件大小
string type = Request.Files[0].ContentType;//文件类型
string name = DateTime.Now.ToString("yyyyMMddhhmmssffff") + "." + "xls";//保存文件的名称
string path = Server.MapPath("~/Content/uploadExcel/") + name; //服务器端保存路径
if (Convert.ToInt32(size) > 2097152)
{
ViewBag.msg = "上传失败文件大于2m";
return View();
}
if (type == "application/vnd.ms-excel" || type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{

HttpPostedFileBase files = Request.Files[0];
files.SaveAs(path);

}