写本文章的目的是为了记录工作中遇到的问题,方便以后遇到可以迅速解决问题
页面标签:
<div class="col-sm-1"><a href="~/ProductsExcel/DownloadTemplate" target="_blank">下载模板</a></div>
后台的方法
public void DownloadTemplate(int type)
{
string filename = "商品颜色.xlsx"; HttpContext.Current.Response.ContentType = "application/ms - excel";//指定下载文件格式 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); filename = HttpContext.Current.Server.MapPath("~/Content/excel/template/" + filename);//下载文件的地址 HttpContext.Current.Response.TransmitFile(filename);//下载
}
//导入excel表格 [HttpPost] public ActionResult ExcelLeadingIn() { var files = Request.Files; string name=files.Keys[0].ToString(); if (files != null && files.Count > 0) { var file = files[0]; string path = Server.MapPath("~/Content/excel/") + file.FileName;//文件保存在当前域名下的Content/excel/中 file.SaveAs(path); string mess = OfferServer.ProductsDetail.InsertExcel(path,file.InputStream,name); return Json(new { state = 1, result = mess }); } return Json(new { state = 0, result = "上传发生错误,请检查后重试" }); }
这里的文件上传我使用的是bootstrap-fileinput,需要了解的哥们可以看我bootstrap分类的文章