<%@ WebHandler Language="C#" Class="FileDragDrop.Web.FileUpload" %>

using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;

namespace FileDragDrop.Web
{
    /// <summary>
    /// StoreFile의 요약 설명입니다.
    /// </summary>
    public class FileUpload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string check = context.Request["filestream"];
            check = Regex.Replace(check, " ", "+");
            byte[] bytes = Convert.FromBase64String(check);

            string tempName = context.Request["filename"];
            string fileName = String.Format("{0}{1}", context.Server.MapPath("./Upload/"), HttpUtility.UrlDecode(tempName).TrimStart(' ')); //context.Server.MapPath(context.Request["filename"]);

            string createDir = String.Format("{0}", context.Server.MapPath("./Upload")); // 경로
            DirectoryInfo di = new DirectoryInfo(createDir);
            if (!di.Exists)
            {
                di.Create();
            }

            FileStream fs;
           
            if (!File.Exists(fileName))
                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
            else
                fs = new FileStream(fileName, FileMode.Append);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            fs.Dispose();
        }

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