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类

相关推荐
Watermelo61738 分钟前
前端如何应对精确数字运算?用BigNumber.js解决JavaScript原生Number类型在处理大数或高精度计算时的局限性
开发语言·前端·javascript·vue.js·前端框架·vue·es6
HebyH_39 分钟前
2025前端面试遇到的问题(vue+uniapp+js+css)
前端·javascript·vue.js·面试·uni-app
EndingCoder3 小时前
2025年JavaScript性能优化全攻略
开发语言·javascript·性能优化
a濯9 小时前
element plus el-table多选框跨页多选保留
javascript·vue.js
H3091910 小时前
vue3+dhtmlx-gantt实现甘特图展示
android·javascript·甘特图
CodeCraft Studio10 小时前
数据透视表控件DHTMLX Pivot v2.1发布,新增HTML 模板、增强样式等多个功能
前端·javascript·ui·甘特图
极小狐11 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
llc的足迹11 小时前
el-menu 折叠后小箭头不会消失
前端·javascript·vue.js
钢铁男儿11 小时前
C# 方法(可选参数)
数据库·mysql·c#
九月TTS11 小时前
TTS-Web-Vue系列:移动端侧边栏与响应式布局深度优化
前端·javascript·vue.js