Ejemplo n.º 1
0
 public JsonResult UploadMultiple(string filename, int albumId, string beskrivelse)
 {
     var count = Request.InputStream.Length;
     var pic = new byte[count];
     Request.InputStream.Read(pic, 0, (int)count);
     try
     {
         var im = Image.FromStream(new MemoryStream(pic));
         var extension = filename.Split('.').Last();
         var imageFormat = ImageUtil.GetImageFormatFromFileExtension(extension);
         var newImage = im.Resize(640, 480);
         var thumbnail = im.Resize(80, 60);
         var album = _db.Albums.Find(albumId) ?? _db.Albums.FirstOrDefault();
         var user = _db.Users.FirstOrDefault(x => x.Username == User.Identity.Name);
         if (album != null)
         {
             var bilde = new Bilde
                             {
                                 AlbumId = album.Id,
                                 Beskrivelse = beskrivelse,
                                 Format = imageFormat.ToString(),
                                 BildeStream = ReadFully(newImage.ToStream(imageFormat)),
                                 Filnavn = filename,
                                 LastetOppAvId = user.UserId,
                                 Navn = filename,
                                 Thumbnail = ReadFully(thumbnail.ToStream(imageFormat))
                             };
             _db.Bilder.Add(bilde);
         }
         _db.SaveChanges();
     }
     catch (Exception)
     {
         throw new FormatException("Wrong format on picture");
     }
     return Json(string.Format("La til {0}", filename));
 }
Ejemplo n.º 2
0
 public ActionResult LastOpp(int? albumId, string beskrivelse)
 {
     if(albumId == null)
     {
         return RedirectToAction("LastOpp", new {warning = "Du må velge et album"});
     }
     var image = Request.Files.Count > 0 ? Request.Files[0] : null;
     var imagesUploadedCounter = 0;
     if (image != null)
     {
         if(ImageUtil.AllowedImageTypes.Contains(image.ContentType))
         {
             var im = Image.FromStream(image.InputStream);
             var imageFormat = ImageUtil.GetImageFormat(image.ContentType);
             var newImage = im.Resize(640, 480);
             var thumbnail = im.Resize(100, 75);
             var album = _db.Albums.Find(albumId) ?? _db.Albums.FirstOrDefault();
             var user = _db.Users.FirstOrDefault(x => x.Username == User.Identity.Name);
             if (album != null)
             {
                 var bilde = new Bilde
                                 {
                                     AlbumId = album.Id,
                                     Beskrivelse = beskrivelse,
                                     Format = imageFormat.ToString(),
                                     BildeStream = ReadFully(newImage.ToStream(imageFormat)),
                                     Filnavn = image.FileName,
                                     LastetOppAvId = user.UserId,
                                     Navn = image.FileName,
                                     Thumbnail = ReadFully(thumbnail.ToStream(imageFormat))
                                 };
                 imagesUploadedCounter++;
                 _db.Bilder.Add(bilde);
             }
             _db.SaveChanges();
         }
     }
     return RedirectToAction("LastOpp", new {success = string.Format("{0} bilde{1} lastet opp.", imagesUploadedCounter, imagesUploadedCounter != 1 ? "r" : string.Empty)});
 }
Ejemplo n.º 3
0
        public JsonResult UploadJqueryMultiple(int? albumId)
        {
            if(albumId == null)
            {
                return null;
            }
            if(Request.Files.Count == 0)
            {
                var imageJsons = new List<JsonUploadResponse>();
                return Json(imageJsons, JsonRequestBehavior.AllowGet);
            }
            var count = Request.Files[0].InputStream.Length;
            var pic = new byte[count];
            var bilde = new Bilde();
            Request.InputStream.Read(pic, 0, (int)count);
            try
            {
                var file = Request.Files[0];
                if (file != null)
                {
                    var im = Image.FromStream(file.InputStream);
                    var extension = file.FileName.Split('.').Last();
                    var imageFormat = ImageUtil.GetImageFormatFromFileExtension(extension);
                    var newImage = im.Resize(800, 600);
                    var thumbnail = im.Resize(80, 60);
                    var user = _db.Users.FirstOrDefault(x => x.Username == User.Identity.Name);
                    bilde = new Bilde
                                {
                                    AlbumId = albumId,
                                    Beskrivelse = "",
                                    Format = imageFormat.ToString(),
                                    BildeStream = ReadFully(newImage.ToStream(imageFormat)),
                                    Filnavn = file.FileName,
                                    LastetOppAvId = user.UserId,
                                    Navn = file.FileName,
                                    Thumbnail = ReadFully(thumbnail.ToStream(imageFormat))
                                };
                    _db.Bilder.Add(bilde);
                    _db.SaveChanges();
                }

            }
            catch (Exception)
            {
                throw new FormatException("Wrong format on picture");
            }
            return
                Json(new[]
                         {
                             new JsonUploadResponse
                                 {
                                     delete_type = "DELETE",
                                     delete_url = "/Bilde/SlettBekreftet/" + bilde.Id,
                                     name = bilde.Navn,
                                     size = bilde.BildeStream.Count(),
                                     thumbnail_url = "/Home/GetThumbnail/" + bilde.Id,
                                     url = "/Home/GetImage/" + bilde.Id
                                 }
                         });
        }