public async Task <IHttpActionResult> Upload(int id) { if (!Request.Content.IsMimeMultipartContent("form-data")) { return(BadRequest("Unsupported media type")); } try { var provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/uploads/")); var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true)); foreach (var header in Request.Content.Headers) { content.Headers.TryAddWithoutValidation(header.Key, header.Value); } await content.ReadAsMultipartAsync(provider); using (var context = new DocumentEntities()) { foreach (var file in provider.FileData) { using (var ts = new TransactionScope()) { var documentMeta = new DocumentMeta { CollectionId = id, FileName = file.Headers.ContentDisposition.Name.Trim(new char[] { '"' }).Replace("&", "and"), UploadTime = DateTime.Now }; context.DocumentMeta.Add(documentMeta); context.SaveChanges(); var documentContent = new DocumentContent { Id = documentMeta.Id, RowGuid = Guid.NewGuid(), Data = new Byte[0] }; context.DocumentContent.Add(documentContent); context.SaveChanges(); using (var stream = File.OpenRead(file.LocalFileName)) { DocumentContentHelper.AddContentData(documentContent.Id, context, stream); } ts.Complete(); } } } return(Ok()); } catch (Exception ex) { return(BadRequest(ex.GetBaseException().Message)); } }
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 Console.WriteLine(ex); } return(handled); }
public static void RecordActivity(DocumentActivityOptions activity, int documentID, int userid) { var db = new DocumentEntities(); var newActivity = new ActivityHistory() { UserID = userid, DocumentID = documentID, InsertDate = DateTime.Now }; switch (activity) { case DocumentActivityOptions.Created: newActivity.StatusID = db.DocumentStatus.First(x => x.Code == DocumentActivityOptions.Created.ToString().ToLower()).Id; break; case DocumentActivityOptions.Viewed: newActivity.StatusID = db.DocumentStatus.First(x => x.Code == DocumentActivityOptions.Viewed.ToString().ToLower()).Id; break; case DocumentActivityOptions.Signed: newActivity.StatusID = db.DocumentStatus.First(x => x.Code == DocumentActivityOptions.Signed.ToString().ToLower()).Id; break; } db.ActivityHistories.Add(newActivity); db.SaveChanges(); }
public IActionResult Delete() { int result = 0; using (var DataContext = new DocumentEntities()) { var db = DataContext.UserSignatures.FirstOrDefault(x => x.UserName == user); DataContext.UserSignatures.Remove(db); result = DataContext.SaveChanges(); } return(Json(result)); }
public async Task <IHttpActionResult> Delete(int id) { using (var context = new DocumentEntities()) { var documentMeta = await context.DocumentMeta.FirstOrDefaultAsync(d => d.Id == id && d.DeleteTime == null); if (documentMeta != null) { documentMeta.DeleteTime = DateTime.Now; context.SaveChanges(); return(Ok()); } } return(NotFound()); }
// DELETE api/collection/5 public async Task <IHttpActionResult> Delete(int id) { using (var context = new DocumentEntities()) { var collection = await context.DocumentCollection.FirstOrDefaultAsync(c => c.Id == id && c.DeleteTime == null); if (collection != null) { collection.DeleteTime = DateTime.Now; context.SaveChanges(); return(Ok()); } } return(NotFound()); }
public IActionResult SignImage(int id, int pageNumber, float x, float y) { using (var DataContext = new DocumentEntities()) { DocumentActivity.RecordActivity(DocumentActivityOptions.Signed, id, userid); var db = new DocumentEntities(); var file = Convert.FromBase64String(db.Documents.Find(id).Base64).ToArray(); var userSignature = DataContext.UserSignatures.FirstOrDefault(sig => sig.UserName == username).SignatureBase64; var userSignatureBytes = Convert.FromBase64String(userSignature.Replace("image/png;base64,", "")); var signedFile = PDFTool.SignFile(file, id, pageNumber, userSignatureBytes, x, y); var doc = db.Documents.First(xx => xx.Id == id); doc.SignedBased64 = signedFile; db.SaveChanges(); return(Content(signedFile)); } }
public IActionResult Update(string signature) { using (var DataContext = new DocumentEntities()) { var usersig = DataContext.UserSignatures.FirstOrDefault(x => x.UserName == user); if (usersig == null) { DataContext.UserSignatures.Add(new UserSignature { SignatureBase64 = signature, UserName = user }); } else { usersig.SignatureBase64 = signature; } DataContext.SaveChanges(); } return(Json(signature)); }
public IActionResult UploadFile(IFormFile file, double x = 1, double y = 1) { var stream = new MemoryStream(); file.CopyTo(stream); //string base64 = PDFTool.ConvertFile(file); //string base64 = Libraries.GemBox.ConvertFile(file); var db = new DocumentEntities(); var newDoc = new Document() { DocumentName = file.FileName, Base64 = Convert.ToBase64String(stream.ToArray()) }; db.Documents.Add(newDoc); db.SaveChanges(); DocumentActivity.RecordActivity(DocumentActivityOptions.Created, newDoc.Id, userid: 1); return(Json(newDoc.Id)); }