using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using Utils.ImageUtils;

public partial class simUploadFile : System.Web.UI.Page
{
    public static string dPath = ConfigurationManager.AppSettings["dPath"];

    private void conSuc(string msg)
    {
        Response.Write("{\"res\":\"1\",\"msg\":\"" + msg + "\"}");
        //Response.End();
    }

    private void conErc(string msg)
    {
        Response.Write("{\"res\":\"0\",\"msg\":\"" + msg + "\"}");
        //Response.End();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Files.Count < 1)
        {
            conErc("空文件!");
            return;
        }
        try
        {
            string ut = Request.QueryString["ut"];
            HttpPostedFile postFile = Request.Files[0];
            if (postFile != null)
            {
                string errMsg = "";
                if (ut != "fp")
                {
                    if (!ImageHandler.CheckImage(postFile, out errMsg))
                    {
                        Response.Write("{\"res\":\"0\",\"msg\":\"不是有效的图片文件!\"}");
                        Response.End();
                        return;
                    }
                }

                //using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(postFile.InputStream))
                //{
                //    result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 400, "", 9, 3);
                //}
                string strMonth = DateTime.Now.ToString("yyyyMM");
                string sPath = Path.Combine(dPath, ut, strMonth);
                if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
                string fName = DateTime.Now.ToFileTime().ToString() + Path.GetExtension(postFile.FileName);
                string saveFile = Path.Combine(sPath, fName);
                //上传文件
                postFile.SaveAs(saveFile);

                conSuc(fName);
                return;
            }
        }
        catch (Exception ex)
        {
            conErc("发生错误!" + CommonHelper.FormatTextArea(ex.Message));
            return;
        }
        conErc("无法上传");
    }
}