private bool HandleUpload(Stream fileStream, string name, int size, string type) { bool handled = false; try { byte[] documentBytes = new byte[fileStream.Length]; fileStream.Read(documentBytes, 0, documentBytes.Length); Document databaseDocument = new Document { CreatedOn = DateTime.Now, FileContent = documentBytes, IsDeleted = false, Name = name, Size = size, Type = type }; using (DocumentEntities databaseContext = new DocumentEntities()) { databaseContext.Documents.Add(databaseDocument); handled = (databaseContext.SaveChanges() > 0); } } catch (Exception ex) { // Oops, something went wrong, handle the exception } return handled; }
private byte[] LoadImage(int id, out string type) { byte[] fileBytes = null; string fileType = null; using (DocumentEntities databaseContext = new DocumentEntities()) { var databaseDocument = databaseContext.Documents.FirstOrDefault(doc => doc.DocumentId == id); if (databaseDocument != null) { fileBytes = databaseDocument.FileContent; fileType = databaseDocument.Type; } } type = fileType; return fileBytes; }