zoukankan      html  css  js  c++  java
  • Word文档下载 流

    后台

     1 public void DownFiletemple(string filepath)
     2         {
     3             FileInfo fileInfo = new FileInfo(filepath);
     4             Response.Clear();
     5             Response.ClearContent();
     6             Response.ClearHeaders();
     7             logger.Trace("测试1:···" + fileInfo.FullName);
     8             Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.FullName);
     9             Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    10             logger.Trace("测试2:··Content-Length/·" + fileInfo.Length.ToString());
    11             Response.AddHeader("Content-Transfer-Encoding", "binary");
    12             Response.AddHeader("Connection", "Keep-Alive");
    13 
    14             Response.ContentType = "application/octet-stream";
    15             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    16             logger.Trace("测试3");
    17             Response.WriteFile(fileInfo.FullName);
    18             logger.Trace("成功");
    19             Response.Flush();
    20             Response.End();
    21 
    22            
    23         }

    异步请求

     1 function btnfiledwon(){
     2     
     3         
     4     var url = 'vshop_CheckOut.aspx?type=ajax&action=FileDown';
     5  
     6    var xhr = new XMLHttpRequest();
     7 
     8    xhr.open('POST', url, true);        // 也可以使用POST方式,根据接口
     9 
    10    xhr.responseType = "blob";    // 返回类型blob
    11 
    12    // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
    13 
    14    xhr.onload = function () {
    15        // 请求完成
    16 
    17        if (this.status === 200) {
    18            // 返回200
    19 
    20            var blob = this.response;
    21 
    22            var reader = new FileReader();
    23 
    24            reader.readAsDataURL(blob);    // 转换为base64,可以直接放入a表情href
    25 
    26            reader.onload = function (e) {
    27 
    28                // 转换完成,创建一个a标签用于下载
    29 
    30                var a = document.createElement('a');
    31 
    32                a.download = '订单合同.docx';
    33 
    34                a.href = e.target.result;
    35 
    36                $("body").append(a);    // 修复firefox中无法触发click
    37 
    38                a.click();
    39 
    40                $(a).remove();
    41 
    42            }
    43 
    44        }
    45 
    46    };
    47 
    48    // 发送ajax请求
    49 
    50    xhr.send()
    51     }
  • 相关阅读:
    华南师范大学应用数学考研真题
    scoped引起的deep与>>>改变elementUI的样式
    7.mogodb索引
    JS的undefined与null,==与===的区别
    vue上传图片到七牛云的思路与实现
    JS用正则处理文件名
    JS使用正则匹配字符串去掉多余符号
    6.mongoDB更新操作
    SSL证书相关之后缀名笔记
    vue一些常用的语法
  • 原文地址:https://www.cnblogs.com/Julyra/p/12739644.html
Copyright © 2011-2022 走看看