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
233
234
235
236
237
238
239
240
241
242
<%@ webhandler Language="C#" class="Upload" %>
 
/**
 * KindEditor ASP.NET
 *
 * 本ASP.NET程序是演示程序,建议不要直接在实际项目中使用。
 * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
 *
 */
 
using System;
using System.Collections;
using System.Web;
using System.IO;
using System.Globalization;
using System.Configuration;
using LitJson;
using Newtonsoft.Json;
using HH.WMS.Common;
using System.Text.RegularExpressions;
 
public class Upload : IHttpHandler
{
    private HttpContext context;
 
    public void ProcessRequest(HttpContext context)
    {
        String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);
        
        //文件保存目录路径
        //String savePath = "../attached/";
        String savePath = "/public/ContentPics/";
        //文件保存目录URL
        //String saveUrl = aspxUrl + "../attached/";
        // String saveUrl = "http://fc.joyinmoney.com:868/public/ContentPics/";//http://192.168.1.169:81/AdminUI/index.aspx
        //定义允许上传的文件扩展名
        Hashtable extTable = new Hashtable();
        extTable.Add("image", "gif,jpg,jpeg,png,bmp");
        extTable.Add("flash", "mp4");
        extTable.Add("media", "mp4");
        extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
 
        //最大文件大小
        int maxSize = 30000000;
        this.context = context;
 
        HttpPostedFile imgFile = context.Request.Files["imgFile"];
        if (imgFile == null)
        {
            showError("请选择文件。");
        }
 
        String dirPath = context.Server.MapPath(savePath);
        if (!Directory.Exists(dirPath))
        {
            showError("上传目录不存在。");
        }
 
        String dirName = context.Request.QueryString["dir"];
        if (String.IsNullOrEmpty(dirName)) {
            dirName = "image";
        }
        if (!extTable.ContainsKey(dirName)) {
            showError("目录名不正确。");
        }
 
        String fileName = imgFile.FileName;
        String fileExt = Path.GetExtension(fileName).ToLower();
 
        if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
        {
            showError("上传文件大小超过限制。");
        }
 
        if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
        {
            showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
        }
 
        //创建文件夹
        dirPath += dirName + "/";//D:\\项目文档\\配件电子商务\\配件商城-2015-08-07\\配件商城-生成静态页\\HanHe.WebUI\\public\\ContentPics\\image/
        savePath += dirName + "/";///public/ContentPics/image/
        if (!Directory.Exists(dirPath)) {
            Directory.CreateDirectory(dirPath);
        }
        String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);//20150826
        dirPath += ymd + "/";//D:\\项目文档\\配件电子商务\\配件商城-2015-08-07\\配件商城-生成静态页\\HanHe.WebUI\\public\\ContentPics\\image/20150826/
        savePath += ymd + "/";///public/ContentPics/image/20150826/
        if (!Directory.Exists(dirPath)) {
            Directory.CreateDirectory(dirPath);
        }
 
        String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;//20150826090252_0834.jpg
        String filePath = dirPath + newFileName;//D:\\项目文档\\配件电子商务\\配件商城-2015-08-07\\配件商城-生成静态页\\HanHe.WebUI\\public\\ContentPics\\image/20150826/20150826090252_0834.jpg
        //如果文件为图片,添加水印
        //如果文件为图片,添加水印
        if (".gif.jpg.jpeg.png.bmp".IndexOf(fileExt) > -1)
        {
            //获取水印图
            string backgroundUrl = context.Request.PhysicalApplicationPath.Replace("\\", "/") + @"Public/WaterMark/";
            if (Directory.Exists(backgroundUrl))
            {
                DirectoryInfo TheFolder = new DirectoryInfo(backgroundUrl);
                if (TheFolder.GetFiles().Length > 0)
                {
 
                    backgroundUrl = backgroundUrl + TheFolder.GetFiles()[0].Name;
                    //给原图添加水印
                    //System.Drawing.Image img = CombinImage(backgroundUrl, System.Drawing.Image.FromStream(imgFile.InputStream)); 
                    //img.Save(filePath);
                    AddWaterPic(System.Drawing.Image.FromStream(imgFile.InputStream), backgroundUrl, filePath);
                }
            }
 
        }
        else
        {
            imgFile.SaveAs(filePath);
        } 
        String fileUrl = savePath + newFileName;///public/ContentPics/image/20150826/20150826090252_0834.jpg
 
        Hashtable hash = new Hashtable();
        hash["error"] = 0;
        hash["url"] = fileUrl;
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(JsonMapper.ToJson(hash));
        context.Response.End();
    }
 
 
    #region 字符串提取
 
    public static string RegexSub(string str, string begin, string end)
    {
        if (string.IsNullOrEmpty(str))
        {
            return "";
        }
        str = str.Replace(begin, "△");
        if (end == "")
        {
            str = str + "▲";
        }
        else
        {
            str = str.Replace(end, "▲");
        }
        return RegexSub(str, "△([^▲]+)▲");
    }
    public static string RegexSub(string str, string zhengze)
    {
        if (str == null)
        {
            return "";
        }
        MatchCollection matchs = new Regex(zhengze, RegexOptions.Singleline | RegexOptions.IgnoreCase).Matches(str);
        return ((matchs.Count == 0) ? "" : matchs[0].Groups[1].Value);
    }
 
    #endregion
 
    #region 添加水印
    /// <summary>
    /// 添加水印
    /// </summary>
    /// <param name="backgroundUrl">水印图片绝对路径</param>
    /// <param name="img">原图Imge对象</param>
    /// <returns></returns>
    private System.Drawing.Image CombinImage(string backgroundUrl, System.Drawing.Image img)
    {
        if (Directory.Exists(backgroundUrl))
        { 
            DirectoryInfo TheFolder = new DirectoryInfo(backgroundUrl);
            if (TheFolder.GetFiles().Length > 0)
            {
 
                backgroundUrl = backgroundUrl + TheFolder.GetFiles()[0].Name;
                System.Drawing.Image imgBack = System.Drawing.Image.FromFile(backgroundUrl);     //相框图片  
 
                System.Drawing.Bitmap pbitmap = new System.Drawing.Bitmap(img);
                //将白色替换成透明
                pbitmap.MakeTransparent(System.Drawing.Color.White);
                img = pbitmap;
                //从指定的System.Drawing.Image创建新的System.Drawing.Graphics        
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgBack);
 
                g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); 
                g.DrawImage(img, 0, 0, img.Width, img.Height);
                GC.Collect();
                return imgBack;
            }
            else
            {
                return img;
            }
        }
        else
        {
            return img;
        }
    }
    #endregion
 
 
    /// <summary>
    /// 在图片上添加图片水印
    /// </summary>
    /// <param name="path">原服务器上的图片路径</param>
    /// <param name="syPicPath">水印图片的路径</param>
    /// <param name="waterPicPath">生成的水印图片存放路径</param>
    public static void AddWaterPic(System.Drawing.Image img, string syPicPath, string waterPicPath)
    {
        System.Drawing.Image image = img;
        System.Drawing.Image waterImage = System.Drawing.Image.FromFile(syPicPath);
        System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image);
        graphic.DrawImage(waterImage, new System.Drawing.Rectangle((image.Width - waterImage.Width) / 2, (image.Height - waterImage.Height) / 2, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, System.Drawing.GraphicsUnit.Pixel);
        graphic.Dispose();
 
        image.Save(waterPicPath);
        image.Dispose();
    }
    
 
 
    private void showError(string message)
    {
        Hashtable hash = new Hashtable();
        hash["error"] = 1;
        hash["message"] = message;
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(JsonMapper.ToJson(hash));
        context.Response.End();
    }
 
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}