ajax实现文件下载

javascript 代码

        function downloadFile() {
            $.ajax({
                type: "get",
                async: false,
                url: '@Href("~/dataStandard/ServerExportMDBFile")',
                data: "",
                success: function (dataUrl) {//dataUrl tempFile/xxx.mdb
            
var 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

 

转载于:https://www.cnblogs.com/yy981420974/p/8193288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值