Exemplo n.º 1
0
        public static Impendulo.Data.Models.File GetFile(int FileID = 0)
        {
            Impendulo.Data.Models.File CurrentFile = null;

            using (var Dbconnection = new MCDEntities())
            {
                CurrentFile = (from a in Dbconnection.Files
                               where a.FileID == FileID
                               select a).FirstOrDefault <Impendulo.Data.Models.File>();
            };
            return(CurrentFile);
        }
Exemplo n.º 2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            using (var Dbconnection = new MCDEntities())
            {
                CurrentFile = (from a in Dbconnection.Files
                               where a.FileID == 1029
                               select a).FirstOrDefault <Impendulo.Data.Models.File>();
            };

            //System.IO.File.WriteAllBytes()
            txtFileName.Text = CurrentFile.FileName + " " + CurrentFile.FileExtension;

            if (checkIfTempFolderExists())
            {
                string path = Directory.GetCurrentDirectory() + "\\Temp" + "\\" + CurrentFile.FileName + "." + CurrentFile.FileExtension;
                System.IO.File.WriteAllBytes(path, CurrentFile.FileImage);
                wbFileDisplay.Navigate(new System.Uri("file:///" + path, System.UriKind.Absolute), "_top", CurrentFile.FileImage, null);
                // wbFileDisplay.Url = new System.Uri("file:///" + path, System.UriKind.Absolute);
                wbFileDisplay.Refresh();
            }
        }
Exemplo n.º 3
0
        // private static OpenFileDialog openFileDialogOpenFiles { get; set; }
        // public static Impendulo.Data.Models.File[] CurrentlyUploadedFiles { get; set; }

        public static List <Impendulo.Data.Models.File> UploadFile(Boolean UseMultipleFileSelect = true, Boolean AutomaicallyAddFileToDatabase = true, Boolean ImagesOnly = false)
        {
            List <Impendulo.Data.Models.File> CurrentlyUploadedFiles = new List <Impendulo.Data.Models.File>();
            OpenFileDialog openFileDialogOpenFiles = new OpenFileDialog();

            openFileDialogOpenFiles.Multiselect = UseMultipleFileSelect;
            if (ImagesOnly)
            {
                openFileDialogOpenFiles.Filter = "Image Files(*.BMP; *.JPG; *.GIF, *.PNG)| *.BMP; *.JPG; *.GIF; *.PNG";
            }
            openFileDialogOpenFiles.ShowDialog();
            if (openFileDialogOpenFiles.FileNames.Count() > 0)
            {
                foreach (string LocalFileName in openFileDialogOpenFiles.FileNames)
                {
                    FileInfo fileInfo = new FileInfo(LocalFileName);

                    Byte[] fileToUpload = new Byte[Convert.ToInt32(fileInfo.Length)];

                    try
                    {
                        fs           = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read);
                        br           = new BinaryReader(fs);
                        fileToUpload = br.ReadBytes(Convert.ToInt32(fileInfo.Length));

                        Char delimiter = '.';
                        var  newFile   = new Impendulo.Data.Models.File
                        {
                            FileName      = fileInfo.Name.Split(delimiter)[0],
                            FileExtension = fileInfo.Extension.Replace(".", ""),
                            ContentType   = "",
                            DateCreated   = DateTime.Now,
                            FileImage     = fileToUpload,
                        };

                        if (br != null)
                        {
                            br.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                        CurrentlyUploadedFiles.Add(newFile);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        if (br != null)
                        {
                            br.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    };
                }
                if (CurrentlyUploadedFiles.Count > 0 && AutomaicallyAddFileToDatabase)
                {
                    using (var DbConnection = new MCDEntities())
                    {
                        DbConnection.Files.AddRange(CurrentlyUploadedFiles);
                        DbConnection.SaveChanges();
                    };
                }
            }
            return(CurrentlyUploadedFiles);
        }