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;

namespace QuickStart_Web
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class photoUpload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            const int MAXFILES = 30;

            // clean up
            string[] files = System.IO.Directory.GetFiles(context.Request.MapPath("Pictures"));
            if (files.Length >= MAXFILES)
            {
                foreach (string fileName in files)
                    System.IO.File.Delete(fileName);
            }

            // get files being uploaded
            for (int i = 0; i < context.Request.Files.Count && i < MAXFILES; i++)
            {
                // get ith file
                HttpPostedFile file = context.Request.Files[i];

                try
                {
                    // check that it really is an image by loading it into an Image object
                    System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);

                    // save file to the Pictures folder
                    string fileName = context.Request.MapPath("Pictures/" + file.FileName);
                    file.SaveAs(fileName);
                }
                catch
                {
                    // doesn't look like a valid image, skip request
                    break;
                }
            }
        }
        public bool IsReusable
        {
            get { return true; }
        }
    }
}
