示例#1
0
        public void UploadDirect(string userId)
        {
            //var headers = HttpContext.Request.Headers;

            //string content = null;
            //using (StreamReader reader = new StreamReader(HttpContext.Request.Form.Files.ToString()))
            //{
            //    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('=');

            //    if (splittedPair[0].StartsWith("faces"))
            //        continue;

            //    results.Add(splittedPair[0], splittedPair[1]);
            //}
            var files   = HttpContext.Request.Form.Files;
            var results = new ImageUploadResult();

            foreach (var file in files)
            {
                if (file != null && file.Length > 0)
                {
                    using (var stream = file.OpenReadStream())
                    {
                        var uploadParams = new ImageUploadParams()
                        {
                            File = new FileDescription(file.Name, stream)
                        };
                        results = _cloudinary.Upload(uploadParams);
                    }

                    Photo p = new Photo()
                    {
                        CreatedAt = results.CreatedAt,
                        Format    = results.Format,
                        Height    = results.Height,

                        PublicId     = results.PublicId,
                        ResourceType = results.ResourceType,
                        SecureUrl    = results.SecureUri.ToString(),
                        Signature    = results.Signature,
                        Type         = results.Type,
                        Url          = results.Uri.ToString(),
                        Version      = Int32.Parse(results.Version),
                        Width        = results.Width,
                        AppUserId    = userId
                    };
                    _repo.AddPhotos(p);
                }
            }
        }