public void OnElectrocardiogramUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            if (e.IsValid) {
                var attachment =
                    new Attachment {
                        FileName = e.UploadedFile.FileName,
                        MimeType = e.UploadedFile.ContentType,
                        StorageFileName = Path.ChangeExtension(
                            Guid.NewGuid().ToString(),
                            Path.GetExtension(e.UploadedFile.FileName)),
                        FileSize = (int)e.UploadedFile.ContentLength
                    };
                attachmentRepository.Add(attachment);
                SaveFile(e, attachment);
                attachmentRepository.Save();

                e.CallbackData = string.Format(
                    "{{ \"id\": {0}, \"name\": \"{1}\", \"link\": \"{2}\" }}",
                    attachment.Id.ToString(),
                    attachment.FileName,
                    Url.Action("GetAttachment", "Attachment", new {id = attachment.Id}));
            }
        }
        public void ElectrocardiogramViewModelToFormData_FullData_WithAttachment()
        {
            //Arrange
            var eViewModel = new ElectrocardiogramFormViewModel() {
                Id = 4,
                ElectrocardiogramActualTime = new DateTime(2011, 10, 11, 9, 10, 11),
                AttachmentId = 14,
            };
            var eFormData = CreateEmptyElectrocardiogramFormData();
            Attachment attachment = new Attachment {
                Id = 14,
                FileName = "test.pdf",
                FileSize = 1234,
                MimeType = "application/pdf",
                StorageFileName = "abcdef.pdf"
            };

            //Act
            formController.MapElectrocardiogramViewModelToFormData(eViewModel, eFormData, attachment);

            //Assert
            Assert.That(eFormData.ElectrocardiogramActualTime.Value,
                        Is.EqualTo(new DateTime(2011, 10, 11, 9, 10, 11).ToString(CultureInfo.InvariantCulture)));

            Assert.That(eFormData.ElectrocardiogramAttachment, Is.Not.Null);
            var file = eFormData.ElectrocardiogramAttachment.File;
            Assert.That(file, Is.Not.Null);
            Assert.That(file.Id, Is.EqualTo(attachment.Id));
            Assert.That(file.FileName, Is.EqualTo(attachment.FileName));
            Assert.That(file.FileSize, Is.EqualTo(attachment.FileSize));
            Assert.That(file.MimeType, Is.EqualTo(attachment.MimeType));
            Assert.That(file.StorageFileName, Is.EqualTo(attachment.StorageFileName));
        }
 private void SaveFile(FileUploadCompleteEventArgs e, Attachment attachment)
 {
     string filePath = Path.Combine(Server.MapPath(AttachmentsFilePath), attachment.StorageFileName);
     e.UploadedFile.SaveAs(filePath, true);
 }
 private Stream GetFileStream(Attachment attachment)
 {
     string filePath = Path.Combine(Server.MapPath(AttachmentsFilePath), attachment.StorageFileName);
     return new FileStream(filePath, FileMode.Open, FileAccess.Read);
 }