예제 #1
0
        public async Task UploadPhotoAsync(Photo photo, HttpPostedFileBase fileToUpload)
        {
            var container = getCloudBlobContainer();

            var fileName = string.Format("Photo-{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(fileToUpload.FileName));

            var blockBlob = container.GetBlockBlobReference(string.Format("{0}/{1}", photo.AlbumID, fileName));

            blockBlob.Properties.ContentType = fileToUpload.ContentType;

            await blockBlob.UploadFromStreamAsync(fileToUpload.InputStream);

            blockBlob.StorageUri.ToString();

            var uriBuilder = new UriBuilder(blockBlob.Uri)
            {
                Scheme = "http"
            };

            photo.FileName = fileName;
            photo.PhotoPath = uriBuilder.ToString();

            //Now upload the created thumbnail.

            var bitMap = Image.FromStream(fileToUpload.InputStream);
            var thumbNail = bitMap.GetThumbnailImage(160, 160, null, IntPtr.Zero);
            var thumbNailFileName = string.Format("{0}/{1}/{2}", photo.AlbumID, "thumbs", fileName);

            var memory = new MemoryStream();

            thumbNail.Save(memory, ImageFormat.Jpeg);

            var byteArray = new byte[memory.Length];
            memory.Position = 0;

            await memory.ReadAsync(byteArray, 0, (int)memory.Length);

            blockBlob = container.GetBlockBlobReference(thumbNailFileName);

            blockBlob.Properties.ContentType = fileToUpload.ContentType;

            await blockBlob.UploadFromByteArrayAsync(byteArray, 0, byteArray.Length);

            uriBuilder = new UriBuilder(blockBlob.Uri)
            {
                Scheme = "http"
            };

            await memory.FlushAsync();

            photo.ThumbnailPath = uriBuilder.ToString();
        }
예제 #2
0
        public async Task<ActionResult> Upload(FormCollection formData)
        {
            var albumID = Convert.ToInt32(formData["Albums"]);

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

                if (fileBase != null)
                {
                    var photo = new Photo
                    {
                        AlbumID = albumID,
                        Description = formData["Description"],
                        ContentType = fileBase.ContentType
                    };

                    var photoService = new PhotoStorageService();

                    await photoService.UploadPhotoAsync(photo, fileBase);

                    unitOfWork.PhotoRepository.Insert(photo);
                    unitOfWork.Save();
                }
            }
            return RedirectToAction("Index", new { AlbumID = albumID });
        }
예제 #3
0
 public PartialViewResult _GetDetailsView(Photo photo)
 {
     return PartialView("_DetailView", photo);
 }