jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Generic;
 
namespace HH.WMS.Utils
{
    /// <summary>
    /// WEB请求上下文信息工具类
    /// </summary>
    public partial class ZHttp
    {
        #region 向客户端发送添加js文件
        /// <summary>
        /// 要添加的js文件的路径
        /// </summary>
        /// <param name="page">要添加js文件的页面</param>
        /// <param name="src">要添加的js文件的路径</param>
        public static void AddJsFile(System.Web.UI.Page page, string src)
        {
            HtmlGenericControl hc = new HtmlGenericControl("script");
            hc.Attributes.Add("type", "text/javascript");
            if (src.StartsWith("~"))
            {
                hc.Attributes.Add("src", page.ResolveClientUrl(src));
            }
            else
            {
                hc.Attributes.Add("src", src);
            }
            page.Header.Controls.Add(hc);
        }
 
        public static void RegisterJsToPage(System.Web.UI.Page page, string src)
        {
            if (!page.ClientScript.IsClientScriptBlockRegistered(src))
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), src, String.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", src));
        }
 
        public static void RegisterJsToPage(System.Web.UI.Page page, string key, string script)
        {
            if (!page.ClientScript.IsClientScriptBlockRegistered(key))
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, script);
        }
 
        /// <summary>
        /// 要添加的js文件的路径
        /// </summary>
        /// <param name="page">要添加js文件的页面</param>
        /// <param name="src">要添加的js文件的路径</param>
        /// <param name="index">要添加的js文件在网页中的位置</param>
        public static void AddJsFile(System.Web.UI.Page page, string src, int index)
        {
            HtmlGenericControl hc = new HtmlGenericControl("script");
            hc.Attributes.Add("type", "text/javascript");
            if (src.StartsWith("~"))
            {
                hc.Attributes.Add("src", page.ResolveClientUrl(src));
            }
            else
            {
                hc.Attributes.Add("src", src);
            }
            page.Header.Controls.AddAt(index, hc);
        }
        #endregion
 
        #region 向客户端添加CSS文件
        /// <summary>
        /// 向客户端添加CSS文件
        /// </summary>
        /// <param name="page">要添加CSS文件的页面</param>
        /// <param name="href">要添加的CSS文件的路径</param>
        public static void AddCssFile(System.Web.UI.Page page, string href)
        {
            HtmlGenericControl hc = new HtmlGenericControl("link");
            hc.Attributes.Add("rel", "stylesheet");
            hc.Attributes.Add("type", "text/css");
            if (href.StartsWith("~"))
            {
                hc.Attributes.Add("href", page.ResolveClientUrl(href));
            }
            else
            {
                hc.Attributes.Add("href", href);
            }
            page.Header.Controls.Add(hc);
        }
        /// <summary>
        /// 向客户端添加CSS文件
        /// </summary>
        /// <param name="page">要添加CSS文件的页面</param>
        /// <param name="href">要添加的CSS文件的路径</param>
        /// <param name="index">要添加的CSS文件在网页中的位置</param>
        public static void AddCssFile(System.Web.UI.Page page, string href, int index)
        {
            HtmlGenericControl hc = new HtmlGenericControl("link");
            hc.Attributes.Add("rel", "stylesheet");
            hc.Attributes.Add("type", "text/css");
            if (href.StartsWith("~"))
            {
                hc.Attributes.Add("href", page.ResolveClientUrl(href));
            }
            else
            {
                hc.Attributes.Add("href", href);
            }
            page.Header.Controls.AddAt(index, hc);
        }
 
        #endregion
 
        #region 向客户端发送文件
        /// <summary>
        /// 向客户端发送文件(在.ashx文件中使用)
        /// </summary>
        /// <param name="context">上下文信息</param>
        /// <param name="fi">文件在服务器上信息</param>
        public static void ResponseFile(HttpContext context, System.IO.FileInfo fi)
        {
            ResponseFile(context, fi.Name, fi);
        }
 
        /// <summary>
        /// 向客户端发送文件(在.ashx文件中使用)
        /// </summary>
        /// <param name="context">上下文信息</param>
        /// <param name="fileName">文件在客户端保存的名称</param>
        /// <param name="fi">文件在服务器上信息</param>
        public static void ResponseFile(HttpContext context, string fileName, System.IO.FileInfo fi)
        {
            context.Response.Clear();
            context.Response.Buffer = false;
            context.Response.ContentType = GetContentType(fi.Extension);
            if (fi != null && fi.Exists)
            {
                if (context.Request.Browser.Browser.ToLower().Contains("ie")) //IE使用编码
                {
                    context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(context.Response.ContentEncoding.GetBytes(fileName)));
                }
                else //其它浏览器不使用编码
                {
                    context.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", fileName));
                }
                context.Response.AddHeader("Connection", "Keep-Alive");
                context.Response.AddHeader("Accept-Ranges", "bytes");
                context.Response.Buffer = false;
 
 
                context.Response.Cache.SetLastModified(fi.LastWriteTime);
                context.Response.Cache.SetETag(fi.LastWriteTime.Ticks.ToString());
 
 
                //ETags和If-None-Match是一种常用的判断资源是否改变的方法。
                //类似于Last-Modified和HTTP-IF-MODIFIED-SINCE。
                //所不同的是Last-Modified和HTTP-IF-MODIFIED-SINCE只判断资源的最后修改时间,而ETags和If-None-Match可以是资源任何的任何属性,如资源的MD5等。
 
                if (context.Request.Headers["If-None-Match"] != null)
                {
                    if (fi.LastWriteTime.Ticks.ToString() == context.Request.Headers["If-None-Match"])
                    {
                        context.Response.StatusCode = 304;
                        context.Response.StatusDescription = "Not Modified";
                        context.Response.End();
                        return;
                    }
                }
 
                //Last-Modified 与If-Modified-Since 都是用于记录页面最后修改时间的 HTTP 头信息,
                //Last-Modified 是由服务器往客户端发送的 HTTP 头,而 If-Modified-Since 则是由客户端往服务器发送的头
                if (context.Request.Headers["If-Modified-Since"] != null)
                {
                    DateTime fromhttptime;
                    DateTime.TryParse(context.Request.Headers["If-Modified-Since"], out fromhttptime);
                    if (fi.LastWriteTime.ToString() == fromhttptime.ToString())
                    {
                        context.Response.StatusCode = 304;
                        context.Response.StatusDescription = "Not Modified";
                        return;
                    }
                }
 
                using (FileStream fs = fi.OpenRead())
                {
                    BinaryReader br = new BinaryReader(fs);
                    try
                    {
                        int bufferlength = 5120;
                        int currentlength = 0;
                        byte[] buffer = new byte[bufferlength];
                        context.Response.AddHeader("Content-Length", fs.Length.ToString());
                        if (context.Response.IsClientConnected)
                        {
                            while (currentlength + bufferlength < fs.Length)
                            {
                                currentlength += br.Read(buffer, 0, buffer.Length);
                                context.Response.BinaryWrite(buffer);
                            }
                            if (fs.Length - currentlength > 0)
                            {
                                buffer = new byte[fs.Length - currentlength];
                                br.Read(buffer, 0, buffer.Length);
                                context.Response.BinaryWrite(buffer);
                            }
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        context.Response.OutputStream.Close();
                        br.Close();
                        fs.Close();
                    }
                    context.Response.Close();
                }
            }
            else
            {
                context.Response.StatusCode = 404;
            }
        }
 
        #endregion
    }
}