Beispiel #1
0
        private void uploadFile(object sender, EventArgs e)
        {
            //open a default directory
            DirectoryInfo info        = new DirectoryInfo(@"../..");
            int           myNodeCount = treeView1.Nodes.Count;

            if (myNodeCount == 1)
            {
                //save this path in a global var
                dirSelected = info.Parent.FullName + "\\" + treeView1.SelectedNode.FullPath;
            }

            //Make use of OpenFileDialog and select images and media
            string         fileContent    = string.Empty;
            string         filePath       = string.Empty;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter           = "All Media Files|*.wav;*.aac;*.wma;*.wmv;*.avi;*.mpg;*.mpeg;*.m1v;*.mp2;*.mp3;*.mpa;*.mpe;*.m3u;*.mp4;*.mov;|Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

            openFileDialog.FilterIndex      = 2;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                filePath = openFileDialog.FileName;

                // Remove path from the file name.
                string fName           = Path.GetFileName(filePath);
                string currentFilePath = filePath.Remove(filePath.IndexOf(fName));

                //copy file from one location to selected dir
                System.IO.File.Copy(Path.Combine(currentFilePath, fName), Path.Combine(dirSelected, fName), true);
                FileInfo fi           = new FileInfo(filePath);
                string   justFileName = Path.GetFileNameWithoutExtension(fi.Name);

                // New file location
                string fullFileName = dirSelected + "\\" + justFileName;
                // Get file extension
                string extn = fi.Extension;
                // Get file size
                var size = fi.Length.ToString();
                // Creation, last access, and last write time
                DateTime creationTime = fi.CreationTime;

                DialogBoxUpload upload = new DialogBoxUpload();
                if (upload.ShowDialog() == DialogResult.Yes)
                {
                    //save file to DB
                    string fiDesc   = upload.FileDescription;
                    string fileTags = upload.FileTags;

                    //use host api
                    var folderId = client.GetFolderId(dirSelected);
                    var sysDate  = DateTime.Now.ToString();

                    //use host api
                    client.AddFile(justFileName, fiDesc, extn, size, fullFileName, fileTags, folderId, sysDate);
                    // Prompt a message to user;
                    MessageBox.Show("File saved to DB", "Success", MessageBoxButtons.OK);
                }
            }
        }