-
Notifications
You must be signed in to change notification settings - Fork 16
/
Default.aspx.cs
275 lines (257 loc) · 7.62 KB
/
Default.aspx.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* ASP实现web上传打印 for Windows
* 使用默认打印机打印
* 对应的IIS应用池需要授权系统账号
* author @NewFuture
*/
using System;
using System.Diagnostics;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Web;
using System.Threading.Tasks;
using System.IO;
public partial class Print : System.Web.UI.Page
{
/// <summary>
/// 日志文件目录
/// </summary>
string LOG_DIR
{
get { return Server.MapPath("~/file/"); }
}
/// <summary>
/// 文件目录
/// </summary>
string FIlE_DIR
{
get { return Server.MapPath("~/file/"); }
}
/// <summary>
/// 上传密码
/// </summary>
protected string Password = System.Configuration.ConfigurationManager.AppSettings["password"];
protected bool needPwd { get { return !String.IsNullOrEmpty(this.Password); } }
/// <summary>
/// 允许的文件后缀
/// </summary>
protected string[] AllowType = { ".pdf", ".jpg", ".png", ".tiff", ".doc", ".docx", ".rtf", ".txt" };
protected void Page_Load()
{
if ("POST" == Request.HttpMethod)
{
//POST 上传
Session["msg"] = this.OnPost();
var url = Request.UrlReferrer;
if (url == null)
{
See_Other(Request.RawUrl);
}
else
{
See_Other(url.ToString());
}
}
else
{
if (!this.needPwd)
{
///密码控件提示
this.Message.Text = "<strong>注意:未设置打印密码任何人可直接打印</strong>";
}
this.Message.Text += Session["msg"];
Session.Remove("msg");
return;
}
}
/// <summary>
/// 处理POST请求的表单
/// </summary>
/// <returns></returns>
protected string OnPost()
{
//验证密码
if (this.needPwd)
{
if (this.Password.Trim() != Request.Form["password"])
{
return "打印密码无效";
}
else
{
//暂时保存密码
Session["pwd"] = Request.Form["password"];
}
}
if (Request.Files.Count < 0)
{
return "无文件( ▼-▼ )";
}
int copies = 0;
if (!int.TryParse(Request.Form["copies"], out copies))
{
return "份数无效";
}
string range = this.GetRange(Request.Form["range"]);
if (range == null)
{
return "打印页码范围[" + Request.Form["range"] + "] 格式错误!";
}
//逐个处理文件
var files = Request.Files;
string msg = "";
for (int i = 0; i < files.Count; ++i)
{
HttpPostedFile file = files[i];
if (this.Upload(file, copies, range))
{
msg += "<div style='color:green;'>" + file.FileName + "已添加到打印队列</div>";
}
else
{
msg += "<div style='color:green;'>" + file.FileName + "打印失败</div>";
}
}
return msg;
}
/// <summary>
/// 303重定向
/// </summary>
/// <param name="url"></param>
public void See_Other(string url)
{
Response.RedirectLocation = url;
Response.StatusCode = 303;
Response.End();
}
/// <summary>
/// 上传打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected bool Upload(HttpPostedFile file, int copies, string range)
{
string type = GetType(file.FileName);
if (type == null)
{
return false;
}
else
{
//保存并打印上传文件
Random r = new Random();
string path = FIlE_DIR + DateTime.UtcNow.ToString("MM-dd_HH-mm-ss_") + r.Next().ToString() + GetType(file.FileName);
file.SaveAs(path);
//打印
return print(path, copies, range, type);
}
}
/// <summary>
/// 检查和获取后缀名
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private string GetType(String filename)
{
string ext = System.IO.Path.GetExtension(filename).ToLower();
return Array.IndexOf(AllowType, ext) == -1 ? null : ext;
}
private string GetRange(string range)
{
if (string.IsNullOrEmpty(range))
{
range = "";
}
else
{
string expr = @"^((\d+(\-\d+)?)\,)*\d+(\-\d+)?$";//^((\d+(\-\d+)?)\,)*\d+(\-\d+)?$
range = range.Trim().Replace(' ', ',').Replace(",", ",").Trim(',');
if (!Regex.IsMatch(range, expr))
{
return null;
}
}
return range;
}
/// <summary>
/// 打印
/// </summary>
/// <param name="path"></param>
/// <param name="copy"></param>
/// <param name="range">范围,1-5</param>
/// <returns></returns>
protected bool print(string path, int copies = 1, string range = null, string type = ".pdf")
{
string cmd, param;
switch (type)
{
case ".pdf":
case ".jpg":
case ".png":
case ".tiff":
//pdf
// https://www.sumatrapdfreader.org/docs/Command-line-arguments.html
cmd = Server.MapPath("~/bin/") + "SumatraPDF.exe";
param = string.Format("-print-to-default -silent -print-settings \"{0}x,{1}\" -appdata \"{3}\" \"{2}\"", copies, range, path, FIlE_DIR);
copies = 1;
break;
default:
//word
cmd = "write";
param = string.Format("/p \"{0}\"", path);
break;
}
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = cmd,
Arguments = param,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
process.OutputDataReceived += async (s, e) => await log(e.Data, "log");
process.ErrorDataReceived += async (s, e) => await log(e.Data, "error");
while (copies-- > 0)
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
return true;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
this.log(ex.ToString(), "exception");
}
return false;
}
/// <summary>
/// 异步记录日志
/// </summary>
/// <param name="msg"></param>
/// <param name="type"></param>
/// <returns></returns>
protected async Task log(string msg, string type = "log")
{
if (String.IsNullOrEmpty(msg))
{
return;
}
string filePath = LOG_DIR + type + ".txt";
byte[] encodedText = System.Text.Encoding.Unicode.GetBytes(msg + "\n\r");
using (FileStream sourceStream = new FileStream(filePath,
FileMode.Append, FileAccess.Write, FileShare.None,
bufferSize: 4096, useAsync: true))
{
await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
};
}
}