예제 #1
0
        public static int InsertFromText(int CompanyID, int MedarbejderID, int TypeID, string Overskrift, string Filename, string Tekst, bool Ekstern = false)
        {
            Korrespondance Korr = new Korrespondance();

            Korr.CompanyID     = CompanyID;
            Korr.MedarbejderID = MedarbejderID;
            Korr.Type          = TypeID;
            Korr.Overskrift    = Overskrift;
            Korr.Status        = 1;
            Korr.Ekstern       = Ekstern;

            Korr.Mime = "text/html"; //text/plain '   getMimeFromFile(Filename) '
            if (!Filename.Contains("."))
            {
                Filename += ".txt";
            }
            Korr.Filename = Filename;
            Korr.Ext      = Filename.Substring(Filename.LastIndexOf(".") + 1);
            Korr.Size     = Tekst.Length;

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Tekst);
            Korr.Blob = bytes;


            return(Korrespondance.Insert(Korr));
        }
예제 #2
0
        public static int InsertFromDisk(int CompanyID, int MedarbejderID, int TypeID, string Overskrift, string Filename, bool DeleteFile = false, bool Ekstern = false)
        {
            if (!File.Exists(Filename))
            {
                throw (new FileNotFoundException("Cannot create attachment because the file was not found : " + Filename, Filename));
            }

            Korrespondance Korr = new Korrespondance();

            Korr.CompanyID     = CompanyID;
            Korr.MedarbejderID = MedarbejderID;
            Korr.Type          = TypeID;
            Korr.Overskrift    = Overskrift;
            Korr.Status        = 1;
            Korr.Ekstern       = Ekstern;

            Korr.Mime = FileIO.getMimeFromFile(Filename); // System.Net.Mime.MediaTypeNames.Application.Pdf ' Text.Plain

            // Load file from harddrive and load into database
            FileInfo fi = new FileInfo(Filename);

            Korr.Filename = fi.Name;
            Korr.Ext      = fi.Extension.Replace(".", "");
            Korr.Size     = (int)fi.Length;
            fi            = null;

            using (FileStream reader = new FileStream(Filename, FileMode.Open))
            {
                byte[] blob = new byte[System.Convert.ToInt32(reader.Length - 1) + 1];
                reader.Read(blob, 0, (int)reader.Length);
                reader.Close();
                //reader.Dispose()
                Korr.Blob = blob;
            }


            if (DeleteFile == true)
            {
                try
                {
                    File.Delete(Filename);
                }
                catch (Exception)
                {
                }
            }

            return(Korrespondance.Insert(Korr));
        }