public void DocumentsTestReadingStream() { const string fileString = "The quick brown fox jumps over the lazy dog."; using (var db = new DataContext()) { //Create the document var document = new Document { FileName = "test.txt", Name = "test", MimeType = "text/plain", Viewable = true, ItemID = Guid.Empty }; db.Documents.Add(document); db.SaveChanges(); try { //Our Test file var textBytes = System.Text.Encoding.UTF8.GetBytes(fileString); //Write the file using (var stream = new DocumentStream(db, document.ID)) { stream.Write(textBytes, 0, textBytes.Length); stream.Flush(); } db.SaveChanges(); //Read the file using (var stream = new DocumentStream(db, document.ID)) { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); var txt = System.Text.Encoding.UTF8.GetString(bytes); Debug.Assert(txt == fileString); } } finally { db.Documents.Remove(document); db.SaveChanges(); } } }
public void CopyData(DataContext db, Guid sourceDocumentID) { using (var sourceStream = new DocumentStream(db, sourceDocumentID)) { using (var destinationStream = new DocumentStream(db, this.ID)) { sourceStream.CopyTo(destinationStream); this.Length = sourceStream.Length; destinationStream.Flush(); } sourceStream.Flush(); } }
public void CopyData(DataContext db, Guid sourceDocumentID) { DateTime contentCreated = DateTime.UtcNow; using (var sourceStream = new DocumentStream(db, sourceDocumentID)) { using (var destinationStream = new DocumentStream(db, this.ID)) { sourceStream.CopyTo(destinationStream); this.Length = sourceStream.Length; destinationStream.Flush(); } sourceStream.Flush(); } db.Database.ExecuteSqlCommand($"UPDATE Documents SET ContentModifiedOn = GETUTCDATE(), ContentCreatedOn = '{ contentCreated }' WHERE ID = '{ this.ID.ToString("D") }'"); }