fetch请求后端返回文件流,并下载。

前端:

<script src="~/layui/layui.js"></script>
<script src="~/Content/js/common/js/vue.min.js"></script>
<script src="~/Content/js/common/js/jquery-1.10.2.min.js"></script>

<style>
</style>

<label class="dgFilterLBBold" style="margin-left: 10px;">日期:</label>
<div class="layui-inline">
    <input type="text" id="Month" class="layui-input" style="width: 150px; margin-right: 10px; height: 30px; margin-top: 5px;">
</div>
<span class="ql-formats"><button class="layui-btn layui-btn-normal layui-btn-sm" onclick="GenReport()" style="margin-top: 3px;width: 80px;">生成报表</button></span>


<script>
    function GenReport() {
      
        fetch('/ReportAirRank/GenReport?beginTime=' + $('#Month').val())
            .then(res => res.blob())
            .then(blob => {
                const url = URL.createObjectURL(blob);
                let a = document.createElement('a');
                a.href = url;
                a.download = "湖北省城市及县域环境空气质量排名.doc";
                a.click();
                a.remove(); 
                //window.open(url, "湖北省城市及县域环境空气质量排名.doc")
                window.URL.revokeObjectURL(url); // 释放掉blob对象
            });
    }
    layui.use(['laydate', 'layer'], function () {
        laydate = layui.laydate;
        layer = layui.layer;
        laydate.render({
            elem: '#Month',
            type: 'month',
            format: 'yyyy-MM',
            done: function (value, date) {

            }
        });
    });

</script>

后端

 public ActionResult GenReport(StatisticQuery model)
        {
            string path = string.Empty;
            path = Server.MapPath("~/Document/Word/空气质量排名情况/省生态环境厅公布" + model.beginTime.ToString("yyyy年M月") + "和1-" + model.beginTime.ToString("M月") + "湖北省城市及县域环境空气质量排名.doc");//上载路径
            string tmppath = Server.MapPath("~/uploads/Reports/全省城市及县域环境空气质量排名情况.docx");//模板
            string fileName = path.Substring(path.LastIndexOf('\\') + 1);
            Aspose.Words.Document doc = new Document(tmppath);
            Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
            if (doc.Range.Bookmarks["SDateTime"] != null)
            {
                doc.Range.Bookmarks["SDateTime"].Text = DateTime.Now.ToString("yyyy年MM月dd日");
            }
            for (int i = 2; i <= 9; i++)
            {
                if (doc.Range.Bookmarks["Y" + i] != null)
                {
                    doc.Range.Bookmarks["Y" + i].Text = model.beginTime.Year.ToString();
                }
            }
            for (int i = 2; i <= 40; i++)
            {
                if (doc.Range.Bookmarks["M" + i] != null)
                {
                    doc.Range.Bookmarks["M" + i].Text = model.beginTime.Month.ToString();
                }
            }
            BaseDao dao = new BaseDao();
            AirRankModel arm = new AirRankModel();


            Type t = typeof(AirRankModel);
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (doc.Range.Bookmarks[property.Name] != null)
                {
                    if (property.GetValue(arm) != null && property.GetValue(arm).ToString() != "")
                    {
                        doc.Range.Bookmarks[property.Name].Text = property.GetValue(arm)?.ToString();
                    }
                    else
                    {
                        doc.Range.Bookmarks[property.Name].Text = "";
                    }
                }
            }

            //FileHelper.DeleteFile(path);
            doc.Save(path, Aspose.Words.SaveFormat.Doc);
            var stream = System.IO.File.OpenRead(path); //Path.GetExtension
            return File(stream, "application/msword", "湖北省城市及县域环境空气质量排名.doc");
            
        }

AirRankModel类

相关推荐
清灵xmf9 分钟前
深入解析 JavaScript 事件委托
前端·javascript·html·事件委托
君莫愁。1 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
天下无贼!1 小时前
2024年最新版TypeScript学习笔记——泛型、接口、枚举、自定义类型等知识点
前端·javascript·vue.js·笔记·学习·typescript·html
Lingbug1 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主2 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
小白小白从不日白2 小时前
react 高阶组件
前端·javascript·react.js
Echo_Lee02 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#
程序员大金2 小时前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
LJ小番茄4 小时前
Vue 常见的几种通信方式(总结)
前端·javascript·vue.js·html