/// <summary> /// ブラウザーを使用してアップロードされた画像があれば、 /// Documentsテーブルに追加してIDを返します。 /// </summary> /// <returns> /// 新しいDocumentオブジェクトのID。 /// アップロードされた画像がなければnull。 /// </returns> internal Guid? GetProjectIconFromRequest() { // 画像ファイルの扱いはWebImageクラスを使用する。 // フォームには "uploadedFile" という名前で格納されているが、 // アクションメソッドのパラメータにはバインドしない。 var image = WebImage.GetImageFromRequest(); if (image == null) { // 画像ファイルとして不適切 return null; } var document = new Document() { ID = Guid.NewGuid(), BinaryData = image.GetBytes(), FileExtension = "." + image.ImageFormat, Uploaded = this.ExecutionContext.Now, User = this.ExecutionContext.CurrentUser }; this.DbContext.Documents.Add(document); return document.ID; }
internal void AttachFile(Message message, HttpPostedFileBase uploadedFile) { if (message == null || uploadedFile == null) { return; } // TODO: 見直し。プロトでは特に検証せずDBに保存する。 var fileName = Path.GetFileName(uploadedFile.FileName); var extension = Path.GetExtension(fileName); var binaryData = GetBytes(uploadedFile.InputStream); if (binaryData.Length > 0) { var document = new Document { ID = Guid.NewGuid(), Uploaded = this.ExecutionContext.Now, User = this.CurrentUser, FileExtension = extension, BinaryData = binaryData, }; this.DbContext.Documents.Add(document); message.AttachedFileName = fileName; message.AttachedDocumentID = document.ID; } }
public ActionResult AddImage(HttpPostedFileBase uploadedFile) { // uploadedFileパラメータは、Get用のアクションメソッドと区別するためのダミーとする。 // 画像ファイルの扱いはWebImageクラスを使用する。 var image = WebImage.GetImageFromRequest(); if (image == null) { // 画像ファイルとして不適切 return RedirectToAction("Index"); } // TODO: トランザクション // 現在のプロジェクトと一緒に、実行ユーザーと所属企業もAttachされる。 db.Configuration.ProxyCreationEnabled = false; db.Projects.Attach(this.Project); db.Entry(this.Project).Reference(x => x.ProjectPage).Query().Include(x => x.PageContents).Load(); var projectPage = this.Project.ProjectPage; if (projectPage == null) { projectPage = new ProjectPage { Created = ctx.Now }; this.Project.ProjectPage = projectPage; } var document = new Document() { ID = Guid.NewGuid(), BinaryData = image.GetBytes(), FileExtension = "." + image.ImageFormat, Uploaded = ctx.Now, User = ctx.CurrentUser }; db.Documents.Add(document); var content = new PageContent { ProjectPage = projectPage, DocumentID = document.ID }; projectPage.PageContents.Add(content); db.SaveChanges(); return RedirectToAction("Index"); }