username@email.com
2021-09-02 9da546cd8de37882147f19f6f090544476bfe5ae
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using CY.Config;
using System.IO;
using CY.Infrastructure;
using Microsoft.Win32;
using System.Diagnostics;
 
namespace CY.WebForm.cs
{
    public class UploadCS
    {
        #region 文件上传
 
        /// <summary>
        /// 上传返回结果类
        /// </summary>
        public class UpFileResult
        {
            private ArrayList _returnfilename = new ArrayList();
            /// <summary>
            /// 保存的文件路径名
            /// </summary>
            public ArrayList returnfilename
            {
                get
                {
                    return _returnfilename;
                }
                set
                {
                    _returnfilename = value;
                }
            }
 
            private ArrayList _returnerror = new ArrayList();
            /// <summary>
            /// 保存失败的描述信息
            /// </summary>
            public ArrayList returnerror
            {
                get
                {
                    return _returnerror;
                }
                set
                {
                    _returnerror = value;
                }
            }
 
            private bool _isEmpty = false;
            /// <summary>
            /// 上传文件是否为空
            /// </summary>
            public bool isEmpty
            {
                get
                {
                    return _isEmpty;
                }
                set
                {
                    _isEmpty = value;
                }
            }
        }
 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="input_name">上传控件ID</param>
        /// <param name="old_path">原文件路径</param>
        /// <returns></returns>
        public static UpFileResult Upload(string input_name, string old_path)
        {
            string path = "/images/Upload/" + DateTime.Now.ToString("yyyyMMdd") + "/";
            return Upload(input_name, path, old_path);
        }
 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="input_name">上传控件ID</param>
        /// <param name="folder_path">保存路径</param>
        /// <param name="old_path">原文件路径</param>
        /// <returns></returns>
        public static UpFileResult Upload(string input_name, string folder_path, string old_path)
        {
            ////保存的文件路径名
            //ArrayList returnfilename = new ArrayList();
            ////保存失败的描述信息
            //ArrayList returnerror = new ArrayList();
            ////上传文件是否为空
            //bool isEmpty = false;
 
            UpFileResult _UpFileResult = new UpFileResult();
            string WebDomain = "http://" + WebInfo.Instance.WebDomain.ToString().Trim('/').Replace("http://", "");
            string UploadFileSize = WebInfo.Instance.UploadFileSize.ToString() ?? "0";
            string UploadFileType = WebInfo.Instance.UploadFileType.ToString() ?? "";
            try
            {
                List<HttpPostedFile> fileList = new List<HttpPostedFile>();
 
                if (string.IsNullOrEmpty(input_name))
                {
                    for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                        fileList.Add(HttpContext.Current.Request.Files[i]);
                }
                else
                {
                    for (int i = 0; i < HttpContext.Current.Request.Files.Keys.Count; i++)
                    {
                        if (input_name == HttpContext.Current.Request.Files.Keys[i].ToString())
                            fileList.Add(HttpContext.Current.Request.Files[i]);
                    }
                }
 
                foreach (HttpPostedFile file in fileList)
                {
                    if (file == null || file.ContentLength == 0)
                    {
                        _UpFileResult.isEmpty = true;
                    }
                    else if (Convert.ToDouble(file.ContentLength) > Convert.ToDouble(UploadFileSize) * 1024)
                        _UpFileResult.returnerror.Add(string.Format("文件不能大于 {0}K", UploadFileSize));
                    else
                    {
                        //当前格式
                        string Extension = System.IO.Path.GetExtension(file.FileName).Replace(".", "");
 
                        //判断格式
                        bool isExtensionRight = false;
                        string[] Extensions = UploadFileType.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < Extensions.Length; i++)
                        {
                            if (Extensions[i].Trim().ToLower() == Extension.ToLower())
                            {
                                isExtensionRight = true;
                                break;
                            }
                        }
 
                        //格式正确
                        if (isExtensionRight)
                        {
                            string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + MathRandom.RandomNumber(4) + "fl." + Extension;
                            filename = "t" + filename.Replace("/", "");
                            string AllFolderPath = HttpContext.Current.Server.MapPath("~" + folder_path);
                            if (!Directory.Exists(AllFolderPath))
                                Directory.CreateDirectory(AllFolderPath);
                            //删除原上传图片
                            if (!string.IsNullOrEmpty(old_path) && File.Exists(HttpContext.Current.Server.MapPath("~" + old_path.Replace(WebDomain, ""))))
                            {
                                //File.Delete(HttpContext.Current.Server.MapPath("~" + old_path.Replace(WebDomain, "")));
                            }
                            //保存文件
                            file.SaveAs(HttpContext.Current.Server.MapPath("~" + folder_path + filename));
                            System.Threading.Thread.Sleep(10);
 
                            _UpFileResult.returnfilename.Add(WebDomain + folder_path + filename);
                        }
                        else
                            _UpFileResult.returnerror.Add(string.Format("格式不在允许的范围内,只允许格式为:{0}", UploadFileType));
                    }
                }
 
            }
            catch
            {
                _UpFileResult.returnerror.Add("上传文件出错");
            }
 
            return _UpFileResult;
        }
 
        /// <summary>
        /// 上传软件版本文件需要压缩包RAR
        /// </summary>
        /// <param name="input_name">上传控件ID</param>
        /// <param name="VerString">版本号</param>
        /// <returns></returns>
        public static UpFileResult UploadSoftVersion(string input_name, string VerString,string SoftCode)
        {
            ////保存的文件路径名
            //ArrayList returnfilename = new ArrayList();
            ////保存失败的描述信息
            //ArrayList returnerror = new ArrayList();
            ////上传文件是否为空
            //bool isEmpty = false;
 
            UpFileResult _UpFileResult = new UpFileResult();
            string WebDomain = "http://soft.cyin.cn";
            string UploadFileSize = "100";
            string UploadFileType = "rar";
            try
            {
                List<HttpPostedFile> fileList = new List<HttpPostedFile>();
 
                if (string.IsNullOrEmpty(input_name))
                {
                    for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                        fileList.Add(HttpContext.Current.Request.Files[i]);
                }
                else
                {
                    for (int i = 0; i < HttpContext.Current.Request.Files.Keys.Count; i++)
                    {
                        if (input_name == HttpContext.Current.Request.Files.Keys[i].ToString())
                            fileList.Add(HttpContext.Current.Request.Files[i]);
                    }
                }
 
                foreach (HttpPostedFile file in fileList)
                {
                    if (file == null || file.ContentLength == 0)
                    {
                        _UpFileResult.isEmpty = true;
                    }
                    else if (Convert.ToDouble(file.ContentLength) > Convert.ToDouble(UploadFileSize) * 1024 * 1024)
                        _UpFileResult.returnerror.Add(string.Format("文件不能大于 {0} M", UploadFileSize));
                    else
                    {
                        //当前格式
                        string Extension = System.IO.Path.GetExtension(file.FileName).Replace(".", "");
 
                        //判断格式
                        bool isExtensionRight = false;
                        string[] Extensions = UploadFileType.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < Extensions.Length; i++)
                        {
                            if (Extensions[i].Trim().ToLower() == Extension.ToLower())
                            {
                                isExtensionRight = true;
                                break;
                            }
                        }
 
                        //格式正确
                        if (isExtensionRight)
                        {
                            string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + MathRandom.RandomNumber(4) + "fl." + Extension;
                            filename = "t" + filename.Replace("/", "");
                            string AllFolderPath = "E:\\DocumentService\\" + SoftCode + "\\";
                            if (!Directory.Exists(AllFolderPath))
                                Directory.CreateDirectory(AllFolderPath);
 
                            //保存文件
                            file.SaveAs(AllFolderPath + "\\" + filename);
                            System.Threading.Thread.Sleep(10);
 
                            UnRAR(AllFolderPath, AllFolderPath, filename);
                            System.Threading.Thread.Sleep(10);
                            try
                            {
                                File.Delete(AllFolderPath + "\\" + filename);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
 
                            _UpFileResult.returnfilename.Add(WebDomain + "/" + VerString + "/" + filename);
                        }
                        else
                            _UpFileResult.returnerror.Add(string.Format("格式不在允许的范围内,只允许格式为:{0}", UploadFileType));
                    }
                }
 
            }
            catch
            {
                _UpFileResult.returnerror.Add("上传文件出错");
            }
 
            return _UpFileResult;
        }
 
        /// <summary>  
        /// 利用 WinRAR 进行解压缩  
        /// </summary>  
        /// <param name="path">文件解压路径(绝对)</param>  
        /// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>  
        /// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>  
        /// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>  
        public static bool UnRAR(string path, string rarPath, string rarName)
        {
            bool flag = false;
            string rarexe;
            RegistryKey regkey;
            Object regvalue;
            string cmd;
            ProcessStartInfo startinfo;
            Process process;
            try
            {
                regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
                regvalue = regkey.GetValue("");
                rarexe = regvalue.ToString();
                regkey.Close();
                rarexe = rarexe.Substring(1, rarexe.Length - 7);
                Directory.CreateDirectory(path);
                //解压缩命令,相当于在要压缩文件(rarName)上点右键 ->WinRAR->解压到当前文件夹  
                cmd = string.Format("x {0} {1} -y", rarName, path);
                startinfo = new ProcessStartInfo();
                startinfo.FileName = rarexe;
                startinfo.Arguments = cmd;
                startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                startinfo.WorkingDirectory = rarPath;
                process = new Process();
                process.StartInfo = startinfo;
                process.Start();
                process.WaitForExit();
                if (process.HasExited)
                {
                    flag = true;
                }
                process.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return flag;
        }
 
        #endregion
 
        #region 获取服务端和物理目录地址
 
        /// <summary>
        /// 获取服务端地址
        /// </summary>
        /// <param name="forumPath"></param>
        /// <returns></returns>
        public static string GetRootUrl(string forumPath)
        {
            forumPath = forumPath.IndexOf("/") == 0 ? forumPath.Substring(1) : forumPath;
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
            int port = HttpContext.Current.Request.Url.Port;
            return string.Format("{0}://{1}{2}{3}/{4}",
                                 HttpContext.Current.Request.Url.Scheme,
                                 HttpContext.Current.Request.Url.Host,
                                 (port == 80 || port == 0) ? "" : ":" + port,
                                 ApplicationPath,
                                 forumPath);
        }
 
        /// <summary>
        /// 获取物理目录地址
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static string GetMapPath(string strPath)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
 
        #endregion
    }
}