using System;
using System.Web;
using System.IO;

public partial class fileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Start"));
        try
        {
            HttpPostedFile fp = Request.Files["up_path"];

            if (Request.QueryString != null && Request.QueryString.Count > 0)
            {
                //O:\fnuImage\201607\19\100501967.jpg
                //Image3\fnuimage\201607\19\100501967.jpg
                //string up_filenm = Request.QueryString["up_path"].ToString();
                string up_filenm = Request.QueryString["up_path"].ToString();
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload: {0}", up_filenm));
                //up_filenm = Server.MapPath(string.Format("./{0}", up_filenm));
                up_filenm = Server.MapPath(string.Format(@".\UPLOAD\{0}", up_filenm));
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload: {0}", up_filenm));
                foreach (string f in Request.Files.AllKeys)
                {
                    HttpPostedFile file = Request.Files[f];

                    CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate START workemr"));
                    //// 윈도우 계정으로 Impersonate
                    ClipSoft.Utility.ImpersonationUtility util = new ClipSoft.Utility.ImpersonationUtility();
                    util.ImpersonationStart(string.Empty, "workemr", "workemr");
                    CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate END"));

                    // 디렉토리 존재여부 체크
                    DirectoryInfo di = new DirectoryInfo(up_filenm.Substring(0, up_filenm.LastIndexOf("\\") + 1));
                    if (di.Exists == false)
                    {
                        di.Create();
                    }

                    // 파일 저장
                    file.SaveAs(up_filenm);

                    // Impersonate 종료
                    util.ImpersonationEnd();
                    CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate CLOSE"));

                    /*
                    //임시 디렉토리에 복사
                    string tempFile = Server.MapPath("./DataTempImage/") + file.FileName;
                    file.SaveAs(tempFile);

                    File.Copy(tempFile, up_filenm, true);                    
                    */
                }
            }
            else if (fp != null)
            {
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("Request.QueryString"));
                fp = Request.Files["up_path"];
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("ANDROID File up_path"));
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(fp.FileName);

                string up_filenm = fp.FileName;
                //up_filenm = Server.MapPath(string.Format("./{0}", up_filenm));
                up_filenm = Server.MapPath(string.Format(@".\UPLOAD\{0}", up_filenm));
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload: {0}", up_filenm));

                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate START workemr"));
                //// 윈도우 계정으로 Impersonate
                ClipSoft.Utility.ImpersonationUtility util = new ClipSoft.Utility.ImpersonationUtility();
                util.ImpersonationStart(string.Empty, "workemr", "workemr");
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate END"));

                // 디렉토리 존재여부 체크
                DirectoryInfo di = new DirectoryInfo(up_filenm.Substring(0, up_filenm.LastIndexOf("\\") + 1));
                if (di.Exists == false)
                {
                    di.Create();
                }

                // 파일 저장
                fp.SaveAs(up_filenm);

                // Impersonate 종료
                util.ImpersonationEnd();
                CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload Impersonate CLOSE"));
            }
            CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm File Upload End"));
        }
        catch (Exception ex)
        {
            CLIP.eForm.Server.Diagnostics.LogHelper.LoggingHandler.Debug(string.Format("CLIP.eForm error: {0}", ex.Message));
            throw ex;
        }
    }
}