下載excel模板,導入數據時須要用到

頁面代碼:ajax

 <form id="form1" enctype="multipart/form-data">
                <div style="float:right">
                    &nbsp;
                    <button type="button" class="btn btn-primary" onclick="$('#fileUpload').click()" id="reviewFile">瀏覽</button>
                    <button class="btn btn-primary" type="button" style="margin-left:5px;height:30px;" id="dataExport">批量導入</button>
                    <input type="button" class="btn btn-primary" style="margin-left:5px;height:30px;" id="downLoad" value="下載模板">
                </div>
                <div style="float:right;margin-top:5px">
                    <input id="fileUpload" name="fileUpload" type="file" style="display:none" />
                    <input id="fileText" type="text" class="form-control" disabled />
                </div>
                <script>
                    $("#fileUpload").change(function () {
                        $("#fileText").val($(this).val());
                    })
                </script>
            </form>
View Code

js代碼:c#

 //下載excel模板
        $("#downLoad").click(function () {
            //方式1
            window.open('/BaseInfoPage/DowntTemplate');

            //方式2 此方式還未解鎖
            //$.ajax({
            //    url: "/BaseInfoPage/DownLoadExcel", //處理頁面的路徑
            //    data: { fileName: "大件運輸許可導入模板.xls" }, //要提交的數據是一個JSON
            //    type: "POST", //提交方式
            //    dataType: "JSON", //返回數據的類型 //TEXT字符串 JSON返回JSON XML返回XML
            //    success: function (data) {
            //        console.log(data);
            //    },
            //    error: function (msg) {
            //        //layer.msg('!', { icon: 1, time: 1000 }, function () {

            //        //});
            //        //layer.msg(msg, { icon: 2, time: 2000 });
            //    }
            //})

            //方式3
            //param = "fileName=" + "大件運輸許可模板.xls";
            //window.location.href = "/BaseInfoPage/DownLoadExcel?" + param;
        })
View Code

c#後臺代碼:app

      /// <summary>
        /// 下載excel模板1
        /// </summary>
        /// <param name="fileName">文件名</param>
        public void DownLoadExcel(string fileName)
        {
            if (Request.Cookies["LoginValue"] == null) Response.Redirect("../Login/LoginPage");

            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "\\ExcelTemplate\\" + fileName + "";//文件路徑
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);//讀取文件路徑
                byte[] buffer = new byte[fs.Length];
                fs.Position = 0;
                fs.Read(buffer, 0, (int)fs.Length);
                Response.Clear();
                Response.AddHeader("Content-Length", fs.Length.ToString());
                Response.ContentType = "application/xls";
                Response.AddHeader("Content-Disposition", "inline;FileName=大件運輸許可導入模板.xls");
                fs.Close();
                Response.BinaryWrite(buffer);
                Response.OutputStream.Flush();
                Response.OutputStream.Close();
                //Response.OutputStream.Write(buffer, 0, (int)fs.Length);
            }
            catch (Exception ex)
            {
                CSysCfg.WriteLog("獲取文檔異常:" + ex.Message);
            }

        }
        /// 下載excel模板2
        public ActionResult DowntTemplate(HttpPostedFileBase file)
        {
            //模板文件的路徑
            string filePath = Server.MapPath("~/ExcelTemplate/大件運輸許可導入模板.xls");////獲取文件路徑
            if (System.IO.File.Exists(filePath))
            {
                string strfileName = Path.GetFileName(filePath);//獲取文件名稱
                return File(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), "application/octet-stream", strfileName);
            }
            else
            {
                return Content("模板文件找不到!請檢查文件是否存在!");//提示用戶
            }
        }
View Code
相關文章
相關標籤/搜索