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类

相关推荐
王哲晓30 分钟前
第三十章 章节练习商品列表组件封装
前端·javascript·vue.js
理想不理想v35 分钟前
‌Vue 3相比Vue 2的主要改进‌?
前端·javascript·vue.js·面试
Envyᥫᩣ1 小时前
C#语言:从入门到精通
开发语言·c#
酷酷的阿云1 小时前
不用ECharts!从0到1徒手撸一个Vue3柱状图
前端·javascript·vue.js
aPurpleBerry2 小时前
JS常用数组方法 reduce filter find forEach
javascript
ZL不懂前端2 小时前
Content Security Policy (CSP)
前端·javascript·面试
乐闻x2 小时前
ESLint 使用教程(一):从零配置 ESLint
javascript·eslint
我血条子呢3 小时前
[Vue]防止路由重复跳转
前端·javascript·vue.js
半开半落3 小时前
nuxt3安装pinia报错500[vite-node] [ERR_LOAD_URL]问题解决
前端·javascript·vue.js·nuxt