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     }

猜你喜欢

转载自www.cnblogs.com/Julyra/p/12739644.html