using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;

namespace ControlExplorer_Web
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler 
        : IHttpHandler
    {
        /*
         *  ------------------------------------------------------------------
         *  This handler process ONE or MORE files in one only request
         *  using the Multipart/Post format that is standard in the Browsers
         *  ------------------------------------------------------------------
         */
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                // get custom parameters..
                string parameters = context.Request.Params["parameters"];

                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    // get the uploaded file, and calculates the full path to save it in the server
                    HttpPostedFile file = context.Request.Files[i];
                    string serverFileName = C1UploaderHelper.GetServerPath(context.Server, file.FileName);

                    if (C1UploaderHelper.ProcessPart(context, file.InputStream, serverFileName, 1, 1))
                    {
                        // add the url of the uploaded file to the response
                        string url = C1UploaderHelper.GetUploadedFileUrl(context, "Handler.ashx", file.FileName);
                        context.Response.Write((i > 0 ? "|" : string.Empty) + url);
                    }
                    else 
                    {
                        C1UploaderHelper.WriteError(context, C1UploaderHelper.ERROR_MESSAGE);
                        break;
                    }
                }
            }
            catch (Exception exc)
            {
                C1UploaderHelper.WriteError(context, exc.Message);
                context.Response.End();
            }
        }



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