Beispiel #1
0
        public static ATTACHMENT AddAttachment(String filename, String description, decimal?display_type, string docScope, int recordType, decimal recordID, string recordStep, string sessionID, Stream file, int Incident_Section)
        {
            ATTACHMENT ret = null;

            try
            {
                using (PSsqmEntities entities = new PSsqmEntities())
                {
                    ATTACHMENT d = new ATTACHMENT();
                    d.FILE_NAME = filename;
                    d.FILE_DESC = description;

                    //To-do: what do we do when company_id is not set, like when they choose this
                    //       from the Business Org master screen?
                    d.COMPANY_ID       = SessionManager.EffLocation.Company.COMPANY_ID;
                    d.OWNER_ID         = SessionManager.UserContext.Person.PERSON_ID;
                    d.UPLOADED_BY      = SessionManager.UserContext.Person.FIRST_NAME + " " + SessionManager.UserContext.Person.LAST_NAME;
                    d.UPLOADED_DT      = WebSiteCommon.CurrentUTCTime();
                    d.LANGUAGE_ID      = (int)SessionManager.UserContext.Person.PREFERRED_LANG_ID;
                    d.ATTACHMENT_SCOPE = docScope;


                    d.RECORD_TYPE      = recordType;
                    d.RECORD_ID        = recordID;              // we might not have the record id when the attaachment is created
                    d.RECORD_STEP      = recordStep;
                    d.SESSION_ID       = sessionID;
                    d.INCIDENT_SECTION = Incident_Section;


                    if (display_type.HasValue)
                    {
                        d.DISPLAY_TYPE = display_type.Value;
                    }

                    if (d.ATTACHMENT_FILE == null)
                    {
                        d.ATTACHMENT_FILE = new ATTACHMENT_FILE();
                    }

                    //read in the file contents
                    file.Seek(0, SeekOrigin.Begin);
                    byte[] bytearray = new byte[file.Length];
                    int    count     = 0;
                    while (count < file.Length)
                    {
                        bytearray[count++] = Convert.ToByte(file.ReadByte());
                    }


                    d.ATTACHMENT_FILE.ATTACHMENT_DATA = bytearray;
                    d.FILE_SIZE = file.Length;

                    // d.DISPLAY_TYPE = Path.GetExtension(filename);

                    entities.AddToATTACHMENT(d);
                    entities.SaveChanges();

                    ret = d;
                }
            }
            catch (Exception e)
            {
                //SQMLogger.LogException(e);
                ret = null;
            }

            return(ret);
        }