<%@ WebHandler Language="c#" Class="File_WebHandler" Debug="true" %>
using System.IO;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using Base;
using UserInfo;
public class File_WebHandler : IHttpHandler
{       
    private const int UploadFileLimit = 4;//上传文件数量限制	
    string url="";
    string _msg = "";
    private string sFile;
    private string dFile;
    private string syFile;
    private string _Url;
    private string LoginName;
    private string CompanyName;
    private int LoginNameId;
    private string Type="";
    private string DirUrl = "";
    private string txtCompanyName;
    private string _isAddWater = "0";
    private string Oldimage;
    private string FilePic;
    private string weburl = "http://localhost:19832/websen/";
    private string reUrl = "";
    public void ProcessRequest(HttpContext context)
    {  
        int iTotal = context.Request.Files.Count;
        if (iTotal == 0)
        {
            _msg = "没有数据";
        }
        else
        {
            int iCount = 0;            
            for (int i = 0; i < iTotal; i++)
            {
                try
                {
                    
                    //取得上传文件信息，并替换其名称
                    HttpPostedFile file = context.Request.Files[i];
                    sFile = Path.GetFileName(file.FileName);
                    sFile = sFile.Substring(sFile.LastIndexOf("."));                
                    string datatime = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");  //为了防止重名，获得日期为文件名  年月日时分秒毫秒                  
                    sFile = datatime + sFile;  //上传后文件的新名
                                        
                    //创建文件夹
                    string strDay = System.DateTime.Now.ToString("yyyyMM");
                    DirUrl = HttpContext.Current.Server.MapPath("~/img/" + strDay + "/");
                    if (!System.IO.Directory.Exists(DirUrl))      //检测文件夹是否存在，不存在则创建
                        System.IO.Directory.CreateDirectory(DirUrl);
                    _Url = "../../img/" + strDay + "/";   //
                    if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                    {
                        //保存文件
                        file.SaveAs(DirUrl + sFile);
                        url = _Url + sFile;
                        reUrl = url;
                        if (context.Request.Form["Type"].ToUpper() != "NEWS")
                        {
                            Solely.ImageWater sy = new Solely.ImageWater(url);
                            bool flag = sy.Watermark();
                            reUrl = sy.ThumbImage;
                        }
                    }
                }
                catch(Exception ex)
                {
                    url = "error";
                    context.Response.Write("<script>window.parent.Finish('" + reUrl + "');</script>");
                }
            }
        }
        context.Response.Write("<script>window.parent.Finish('" + reUrl + "');</script>");
      
   }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}