1. 程式人生 > >ajax實現文件下載

ajax實現文件下載

mappath 相對路徑 .ajax 文件下載 urn tostring ons tempfile mode

javascript 代碼

        function downloadFile() {
            $.ajax({
                type: "get",
                async: false,
                url: ‘@Href("~/dataStandard/ServerExportMDBFile")‘,
                data: "",
                success: function (dataUrl) {//dataUrl tempFile/xxx.mdbvar urlArray = window.location.href.split("/");
            //urlArray[0]->http
urlArray[2]->域名:192.168.0.66 urlArray[3]->Test
var fileUrl = urlArray[0] + "//" + urlArray[2] + "/" + urlArray[3] + "/" + dataUrl;
            //通過window.open(urlAddess) 直接打開js拼接出來的 js window.open(fileUrl); }, error:
function (url) { alert(
"錯誤:" + url); } }) }

C# 代碼

技術分享圖片
        public string ExportMDBFile()
        {
            Random random = new Random();

            string relativeServerPath = Server.MapPath("../");
            string relativePath = @"Config\DataStandard.mdb";
            string
filePath = Path.Combine(relativeServerPath, relativePath); // DS + 日期 + 0-9999隨機數 string relativeGoalPath = @"tempFile\DS" + DateTime.Now.ToString("yyyyMMdd") + random.Next(0, 9999).ToString() + ".mdb"; string goalPath = Path.Combine(relativeServerPath, relativeGoalPath); // 復制文件到指定路徑 System.IO.File.Copy(filePath, goalPath); //初始化 OnStr shwyTabObject.Configuation.Init(goalPath); List<lirObjModel.Boj_SYS_CODE> codeList = null; List<lirObjModel.Boj_SYS_TYPE> typeList = null; using (lirObj.BojModelsVisitor visitor = new lirObj.BojModelsVisitor()) { codeList = lirObjModel.Boj_SYS_CODE.List(visitor); typeList = lirObjModel.Boj_SYS_TYPE.List(visitor); } using (shwyObj.BojModelsVisitor visitor = new shwyObj.BojModelsVisitor()) { foreach (var item in codeList) { shwyObj.BOJModels.Boj_SYS_CODE code = shwyObj.BOJModels.Boj_SYS_CODE.Create(); code.Code = item.Code; code.Id = item.Id; code.Name = item.Name; code.Title = item.Title; code.Typeid = item.Typeid; shwyObj.BOJModels.Boj_SYS_CODE.Insert(visitor, code); } foreach (var item in typeList) { shwyObj.BOJModels.Boj_SYS_TYPE type = shwyObj.BOJModels.Boj_SYS_TYPE.Create(); type.ID = item.ID; type.NAME = item.NAME; shwyObj.BOJModels.Boj_SYS_TYPE.Insert(visitor, type); } } //第一個 Replace 替換 文件呢絕對路徑為相對路徑 第二個 Replace 將 \\ 替換為 / return goalPath.Replace(relativeServerPath, "").Replace("\\", "/"); }
View Code

ajax實現文件下載