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

58 篇文章 2 订阅

前端: 

<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类 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值