Ejemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            string jsonObj = null;
            try
            {
                string fileName = null;
                int fileLength = 0;
                Stream stream = null;
                if (context.Request.Files.Count > 0)
                {
                    var file = context.Request.Files["qqfile"];
                    if (file != null)
                    {
                        fileName = Path.GetFileName(file.FileName);
                        fileLength = file.ContentLength;
                        stream = file.InputStream;
                    }
                }
                else
                {
                    fileName = context.Request.QueryString["qqfile"] ?? context.Request.Headers["X-File-Name"];
                    int.TryParse(context.Request.Headers["Content-Length"], out fileLength);
                    stream = context.Request.InputStream;
                }

                if (stream != null && stream.Length > 0)
                {
                    var fileExtension = Path.GetExtension(fileName).Trim();
                    var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName).Trim();
                    if (String.IsNullOrWhiteSpace(fileName)) fileName = "noname";

                    var fileBytes = new byte[fileLength];
                    stream.Read(fileBytes, 0, fileLength);

                    var photo = new Photo
                    {
                        FileName = fileNameWithoutExtension,
                        FileExtention = fileExtension,
                        ContentType = "image/jpeg",
                        FileSize = fileLength,
                        Original = new Original { Content = fileBytes },
                        UploadDate = DateTime.Now
                    };
                    var service = new PhotoService();
                    var id = service.Upload(photo).Id;
                    context.Response.ContentType = "text/html";
                    jsonObj = js.Serialize(new { success = true, Id = id });
                }
            }
            catch (Exception ex)
            {
                jsonObj = js.Serialize(new { success = false, error = "Ошибка при передаче файла. " + ex.Message });
            }
            context.Response.Write(jsonObj);
        }
Ejemplo n.º 2
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["Id"] == null)
            {
                context.Response.End();
                return;
            }
            var count = 6;
            int id;
            int.TryParse(context.Request.QueryString["Id"], out id);
            if (id == 0) //Если вызывается первый раз (при загрузке формы)
            {
                id = int.MaxValue;
            }

            var service = new PhotoService();
            var photoList = service.GetPhotoesList(id, count).ToList();
            //var photoList = service.GetFakePhotoesList().ToList();
            for (int i = 0; i < photoList.Count; i++)
            {
                context.Response.Write("<li");
                if (i == photoList.Count - 1)
                {
                    context.Response.Write(" customId=\"" + photoList[i].Id + "\" class=\"waypoint\"");
                }
                context.Response.Write("><div class=\"picture\"><a href=\"Picture.aspx?id=");
                context.Response.Write(photoList[i].Id.ToString());
                context.Response.Write("\" target=\"_blank\">");
                context.Response.Write("<img src=\"Thumbnail.aspx?id=");
                context.Response.Write(photoList[i].Id.ToString());
                context.Response.Write("\" alt=\"");
                context.Response.Write(photoList[i].FileName);
                context.Response.Write("\">");
                context.Response.Write("</a></li>");
            }
        }