<%@ 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;
|
}
|
}
|
}
|