Beispiel #1
0
        private async Task<bool> HandleUpload(Stream fileStream, string name, int size, string type, int id)
        {
            bool handled = false;

            try
            {
                byte[] documentBytes = new byte[fileStream.Length];
                fileStream.Read(documentBytes, 0, documentBytes.Length);

                Image image = new Image
                {
                    ShelterId = id,
                    CreatedOn = DateTime.Now,
                    FileContent = documentBytes,
                    IsDeleted = false,
                    Name = name,
                    Size = size,
                    Type = type
                };

                using (SheltersContext db = new SheltersContext())
                {
                    db.Images.Add(image);
                    handled = (await db.SaveChangesAsync()> 0);
                    await db.SaveRecord(image, User.Identity.Name, "Image added");
                }
            }
            catch (Exception ex)
            {
                // Oops, something went wrong, handle the exception
            }

            return handled;
        }
Beispiel #2
0
 private byte[] LoadImage(int id, out string type)
 {
     byte[] fileBytes = null;
     string fileType = null;
     using (SheltersContext db = new SheltersContext())
     {
         var image = db.Images.FirstOrDefault(i => i.Id == id);
         if (image != null)
         {
             fileBytes = image.FileContent;
             fileType = image.Type;
         }
     }
     type = fileType;
     return fileBytes;
 }