.net 下載excel文件和上傳文件


 

寫本文章的目的是為了記錄工作中遇到的問題,方便以后遇到可以迅速解決問題

頁面標簽:

 <div class="col-sm-1"><a href="~/ProductsExcel/DownloadTemplate" target="_blank">下載模板</a></div>

后台的方法

        public void DownloadTemplate()
        {
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分類的文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM