public void AddDocument() { try { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { HttpPostedFile file = httpRequest.Files[0]; MemberDocumentModel model = new MemberDocumentModel(); if (httpRequest.Form["model"] != null && !string.IsNullOrWhiteSpace(httpRequest.Form["model"])) { model = JsonConvert.DeserializeObject <MemberDocumentModel>(httpRequest.Form["model"]); // get file bytes using (BinaryReader binaryReader = new BinaryReader(file.InputStream)) { model.File.FileBytes = binaryReader.ReadBytes(file.ContentLength); } BL.Members.InsertDocument(model); } } } catch (Exception ex) { throw new HttpResponseException( new HttpResponseMessage() { StatusCode = HttpStatusCode.Forbidden, ReasonPhrase = ex.Message }); } }
public static void InsertDocument(MemberDocumentModel model) { DocumentModel docModel = Documents.ConvertMemberDocumentModelToDocModel(model); // first save file to file system File.WriteAllBytes(docModel.DocumentPhysicalPath, docModel.Bytes); try { // then insert doc data into DB DB.Members.InsertDocument(model.MemberID, docModel); } catch (Exception ex) { File.Delete(docModel.DocumentPhysicalPath); throw ex; } }
public static DocumentModel ConvertMemberDocumentModelToDocModel(MemberDocumentModel model) { DocumentModel docModel = new DocumentModel(); //docModel.DocumentName = Path.GetFileNameWithoutExtension(model.File.FileName); docModel.DocumentName = model.DocumentName; docModel.DocumentDesc = model.DocumentDesc; docModel.DocumentTypeID = model.DocumentTypeID; docModel.DocumentCodedName = Guid.NewGuid().ToString(); docModel.CreationDate = DateTime.Now; // document file name & extension docModel.DocumentFileName = Path.GetFileNameWithoutExtension(model.File.FileName); docModel.DocumentFileExtension = Path.GetExtension(model.File.FileName).Substring(1); // get file bytes //docModel.Bytes = Helpers.GetBytesFromDataUrl(model.File.DataUrl); docModel.Bytes = model.File.FileBytes; // determine folder to store the document docModel.DocumentPhysicalPath = string.Format(@"{0}{1}\{2}", GetPhysicalPath(), string.Empty, string.Format("{0}.tim", docModel.DocumentCodedName)); // document path (relative to the document store path) docModel.DocumentPath = string.Format(@"{0}\{1}", string.Empty, docModel.DocumentCodedName); if (model.UserID.HasValue) { docModel.CreatedByUserID = model.UserID; } // document metadata if (model.DocMetadata != null) { docModel.Metadata = new DocumentMetadataModel(); docModel.Metadata.ExpiryDate = model.DocMetadata.ExpiryDate; } return(docModel); }