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

public partial class uploadImg : 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)
    {
        string err = "{\"res\":\"0\",\"msg\":\"上传失败!\"}";
        //if (CurrentUser==null)
        //{
        //    Response.Write(err);
        //    return;
        //}
        if (Request.Files.Count < 1)
        {
            Response.Write(err);
            return;
        }
        string idx = Request.QueryString["idx"];
        if (idx == "")
        {
            Response.Write(err);
            return;
        }
        //string fn = DateTime.Now.ToString("yyyyMMddHHmmss");
        string ut = Request.QueryString["ut"];
        try
        {
            HttpPostedFile postFile = Request.Files[0];
            if (postFile != null)
            {
                string errMsg = "";
                if (!ImageHandler.CheckImage(postFile, out errMsg)) return;
                string sPath = Path.Combine(dPath, ut);
                string fileName = postFile.FileName;
                string saveFile = Path.Combine(sPath, fileName);
                string result = "";
                using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(postFile.InputStream))
                {
                    result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 800, "", 9, 3);
                }
                Response.Write("{\"res\":\"1\",\"msg\":\"上传成功！\",\"ridx\":\"" + idx + "\",\"fn\":\"" + SecurityHelper.EncodingBase64(fileName) + "\"}");
                return;
            }
        }
        catch(Exception ex)
        {
            Response.Write(err+ex.Message);
        }
        Response.Write(err);
    }
}