Пример #1
0
        private void UploadWholeFile(HttpContextBase requestContext, List <ViewDataUploadFilesResult> statuses, int?parentid, int?childid)
        {
            string guid    = "";
            var    request = requestContext.Request;
            //GalleryDetail img;
            string URLthumb = "";

            for (int i = 0; i < request.Files.Count; i++)
            {
                var    file         = request.Files[i];
                string pathOnServer = Path.Combine(StorageRoot);
                string fileThumb    = "";


                guid = DateTime.Now.ToString("HHmmss");//Guid.NewGuid().ToString();

                var fullPath = Path.Combine(pathOnServer, Path.GetFileName(guid + "_" + file.FileName));

                file.SaveAs(fullPath);

                //Create thumb
                string[] imageArray = file.FileName.Split('.');
                if (imageArray.Length != 0)
                {
                    string extansion = imageArray[imageArray.Length - 1].ToString().ToLower();
                    if (extansion != "jpg" && extansion != "png") //Do not create thumb if file is not an image
                    {
                    }
                    else
                    {
                        var ThumbfullPath = Path.Combine(pathOnServer, "_thumbs");
                        fileThumb = guid + "_" + file.FileName.Remove(file.FileName.Length - 4, 4) + ".80x80.jpg";
                        var ThumbfullPath2 = Path.Combine(ThumbfullPath, fileThumb);
                        using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(fullPath)))
                        {
                            var thumbnail = new WebImage(stream).Resize(200, 200);
                            thumbnail.Save(ThumbfullPath2, "jpg");
                            URLthumb = ThumbfullPath2;
                        }
                    }
                }

                ProductImageCategory img = new ProductImageCategory();
                img.Created          = DateTime.Now;
                img.ImagesThumb      = fileThumb;
                img.CategoryIDParent = parentid;
                if (childid > 0)
                {
                    img.CategoryID = childid;
                }
                img.Title    = file.FileName.Substring(0, file.FileName.Length - 4);
                img.URLImage = guid + "_" + file.FileName;
                db.ProductImageCategories.Add(img);
                db.SaveChanges();


                statuses.Add(UploadResult(guid + "_" + file.FileName, file.ContentLength, guid + "_" + file.FileName));
            }
        }
Пример #2
0
        private void UploadPartialFile(string fileName, HttpContextBase requestContext, List <ViewDataUploadFilesResult> statuses, int?parentid, int?childid)
        {
            var request = requestContext.Request;

            if (request.Files.Count != 1)
            {
                throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
            }
            var          file          = request.Files[0];
            var          inputStream   = file.InputStream;
            string       patchOnServer = Path.Combine(StorageRoot);
            var          fullName      = Path.Combine(patchOnServer, Path.GetFileName(file.FileName));
            var          ThumbfullPath = Path.Combine(fullName, Path.GetFileName(file.FileName + ".80x80.jpg"));
            ImageHandler handler       = new ImageHandler();

            var ImageBit = ImageHandler.LoadImage(fullName);

            handler.Save(ImageBit, 20, 20, 10, ThumbfullPath);
            using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write))
            {
                var buffer = new byte[1024];

                var l = inputStream.Read(buffer, 0, 1024);
                while (l > 0)
                {
                    fs.Write(buffer, 0, l);
                    l = inputStream.Read(buffer, 0, 1024);
                }
                fs.Flush();
                fs.Close();
            }

            ProductImageCategory img = new ProductImageCategory();

            img.Created          = DateTime.Now;
            img.CategoryIDParent = parentid;
            img.CategoryID       = childid;
            img.Title            = file.FileName.Substring(0, file.FileName.Length - 4);
            img.ImagesThumb      = file.FileName;
            img.URLImage         = file.FileName + ".80x80.jpg";
            db.ProductImageCategories.Add(img);
            db.SaveChanges();

            statuses.Add(UploadResult(file.FileName, file.ContentLength, file.FileName));
        }