public virtual JsonResult AddAttachment(int? id) { if (User.Identity.IsAuthenticated) { if (Request.Files.Count > 0) { HttpPostedFileBase userPostedFile = Request.Files[0]; { var attachment = new TicketAttachment(); string fName = Path.GetFileName(userPostedFile.FileName); attachment.FileName = fName; attachment.FileSize = userPostedFile.ContentLength; string mtype = userPostedFile.ContentType; attachment.FileType = (string.IsNullOrEmpty(mtype) ? "application/octet-stream" : mtype); byte[] fileContent = new byte[userPostedFile.ContentLength]; userPostedFile.InputStream.Read(fileContent, 0, userPostedFile.ContentLength); var isDemo = (bool)Settings.ApplicationSettings.GetSettingValue("IsDemo"); if (isDemo) { attachment.FileContents = System.Text.Encoding.UTF8.GetBytes("The demo does not store upload content..."); } else { attachment.FileContents = fileContent; } try { System.Threading.Thread.Sleep(2000); int fileId = Tickets.AddPendingAttachment(id, attachment); return new JsonResult() { ContentType = "text/plain", Data = new { success = true, id = fileId.ToString() } }; } catch { return new JsonResult(); } } } throw new InvalidOperationException("No file data was uploaded"); } throw new InvalidOperationException("The user is not authenticated."); }
/// <summary> /// Adds a pending attachment. /// </summary> /// <param name="attachment">The attachment to add.</param> /// <param name="commit">if set to <c>true</c> save pending attachment to DB.</param> /// <returns></returns> public bool AddPendingAttachment(TicketAttachment attachment, bool commit) { ctx.TicketAttachments.AddObject(attachment); if (commit) { ctx.SaveChanges(); if (attachment.Ticket != null)//new tickets page can add and remove attachments before the ticket physically exists in the DB { SavingTicketChanges(attachment.Ticket); } } return true; }
/// <summary> /// Deprecated Method for adding a new object to the TicketAttachments EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToTicketAttachments(TicketAttachment ticketAttachment) { base.AddObject("TicketAttachments", ticketAttachment); }
/// <summary> /// Create a new TicketAttachment object. /// </summary> /// <param name="fileId">Initial value of the FileId property.</param> /// <param name="fileName">Initial value of the FileName property.</param> /// <param name="fileSize">Initial value of the FileSize property.</param> /// <param name="fileType">Initial value of the FileType property.</param> /// <param name="uploadedBy">Initial value of the UploadedBy property.</param> /// <param name="uploadedDate">Initial value of the UploadedDate property.</param> /// <param name="fileContents">Initial value of the FileContents property.</param> /// <param name="isPending">Initial value of the IsPending property.</param> public static TicketAttachment CreateTicketAttachment(global::System.Int32 fileId, global::System.String fileName, global::System.Int32 fileSize, global::System.String fileType, global::System.String uploadedBy, global::System.DateTime uploadedDate, global::System.Byte[] fileContents, global::System.Boolean isPending) { TicketAttachment ticketAttachment = new TicketAttachment(); ticketAttachment.FileId = fileId; ticketAttachment.FileName = fileName; ticketAttachment.FileSize = fileSize; ticketAttachment.FileType = fileType; ticketAttachment.UploadedBy = uploadedBy; ticketAttachment.UploadedDate = uploadedDate; ticketAttachment.FileContents = fileContents; ticketAttachment.IsPending = isPending; return ticketAttachment; }
/// <summary> /// Adds the pending attachment. /// </summary> /// <param name="ticketId">The ticket id for an existing ticket, or null if pending attachment is for a new ticket that hasn't been created yet.</param> /// <param name="file">The file.</param> /// <returns>the FileId of the pending attachment</returns> public int AddPendingAttachment(int? ticketId, TicketAttachment file) { if (ticketId.HasValue) { file.TicketId = ticketId.Value; } file.IsPending = true; file.UploadedBy = Security.CurrentUserName; file.UploadedDate = DateTime.Now; Repository.AddPendingAttachment(file, true); return file.FileId; }