예제 #1
0
        public void UploadDirect()
        {
            var headers = HttpContext.Request.Headers;

            string content = null;
            using (StreamReader reader = new StreamReader(HttpContext.Request.InputStream))
            {
                content = reader.ReadToEnd();
            }

            if (String.IsNullOrEmpty(content)) return;

            Dictionary<string, string> results = new Dictionary<string, string>();

            string[] pairs = content.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var pair in pairs)
            {
                string[] splittedPair = pair.Split('=');

                results.Add(splittedPair[0], splittedPair[1]);
            }

            Photo p = new Photo()
            {
                Bytes = Int32.Parse(results["bytes"]),
                CreatedAt = DateTime.ParseExact(HttpUtility.UrlDecode(results["created_at"]), "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture),
                Format = results["format"],
                Height = Int32.Parse(results["height"]),
                Path = results["path"],
                PublicId = results["public_id"],
                ResourceType = results["resource_type"],
                SecureUrl = results["secure_url"],
                Signature = results["signature"],
                Type = results["type"],
                Url = results["url"],
                Version = Int32.Parse(results["version"]),
                Width = Int32.Parse(results["width"]),
            };

            PhotoAlbumContainer album = new PhotoAlbumContainer();

            album.Photos.Add(p);

            album.SaveChanges();
        }
예제 #2
0
        public ActionResult UploadServer()
        {
            PhotoAlbumContainer album = new PhotoAlbumContainer();
            Dictionary<string, string> results = new Dictionary<string, string>();

            for (int i = 0; i < HttpContext.Request.Files.Count; i++)
            {
                var file = HttpContext.Request.Files[i];

                if (file.ContentLength == 0) return PartialView("Upload", new Model(m_cloudinary));

                var result = m_cloudinary.Upload(new ImageUploadParams()
                {
                    File = new CloudinaryDotNet.Actions.FileDescription(file.FileName,
                        file.InputStream),
                    Tags = "backend_photo_album"
                });

                foreach (var token in result.JsonObj.Children())
                {
                    if (token is JProperty)
                    {
                        JProperty prop = (JProperty)token;
                        results.Add(prop.Name, prop.Value.ToString());
                    }
                }

                Photo p = new Photo()
                {
                    Bytes = (int)result.Length,
                    CreatedAt = DateTime.Now,
                    Format = result.Format,
                    Height = result.Height,
                    Path = result.Uri.AbsolutePath,
                    PublicId = result.PublicId,
                    ResourceType = result.ResourceType,
                    SecureUrl = result.SecureUri.AbsoluteUri,
                    Signature = result.Signature,
                    Type = result.JsonObj["type"].ToString(),
                    Url = result.Uri.AbsoluteUri,
                    Version = Int32.Parse(result.Version),
                    Width = result.Width,
                };

                album.Photos.Add(p);
            }

            album.SaveChanges();

            return PartialView("UploadSucceeded",
                new DictionaryModel(m_cloudinary, results));
        }