Пример #1
0
        private void button9_Click(object sender, EventArgs e)
        {
            FileStream stream;
            OpenFileDialog ofd = new OpenFileDialog();
            var client = new MongoClient("mongodb://localhost");
            var database = client.GetDatabase("docs");
            var fs = new GridFSBucket(database);
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                stream = new System.IO.FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                //SoundPlayer simpleSound = new SoundPlayer(stream);
                //simpleSound.Play();
                var split = ofd.SafeFileName.Split('.');
                if(split[1] != "mp3")
                {
                    MessageBox.Show("Izaberite mp3 fajl!");
                    return;
                }
                GridFSUploadOptions opcije = new GridFSUploadOptions();
                opcije.ContentType = "audio/mp3";
                opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length)/4;

                int duzina = Convert.ToInt32(stream.Length);
                byte[] bajtovi = new byte[duzina];
                stream.Seek(0, SeekOrigin.Begin);
                int bytesRead = stream.Read(bajtovi, 0, duzina);

                fs.UploadFromBytes(ofd.SafeFileName, bajtovi, opcije);

            }
        }
Пример #2
0
        public static bool AddSoundToGridFS(FileStream stream,string imePesme,string format)
        {
            try
            {
                var client = new MongoClient("mongodb://localhost");
                var database = client.GetDatabase("docs");
                var fs = new GridFSBucket(database);

                GridFSUploadOptions opcije = new GridFSUploadOptions();
                opcije.ContentType = "audio/"+format;
                opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length) / 4;
                
                int duzina = Convert.ToInt32(stream.Length);
                byte[] bajtovi = new byte[duzina];
                stream.Seek(0, SeekOrigin.Begin);
                int bytesRead = stream.Read(bajtovi, 0, duzina);

                fs.UploadFromBytes(imePesme, bajtovi, opcije);

                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
Пример #3
0
        private void button8_Click(object sender, EventArgs e)
        {
            Image slika;
            FileStream stream;
            var client = new MongoClient("mongodb://localhost");
            var database = client.GetDatabase("docs");
            var fs = new GridFSBucket(database);
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                stream = new System.IO.FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                slika = Image.FromStream(stream);

                GridFSUploadOptions opcije = new GridFSUploadOptions();
                opcije.ContentType = "image/jpg";
                opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length);

                int duzina = Convert.ToInt32(stream.Length);
                byte[] bajtovi = new byte[duzina];
                stream.Seek(0, SeekOrigin.Begin);
                int bytesRead = stream.Read(bajtovi, 0, duzina);

                fs.UploadFromBytes("Test", bajtovi, opcije);

            }
            MessageBox.Show("done");
        }
Пример #4
0
        public static bool AddImageToGridFS(Image slika,string imeSlike,string format)
        {
            try
            {
                //provera da li postoji slika
                //var client = new MongoClient("mongodb://localhost");
                //var database = client.GetDatabase("docs");
                //var fs = new GridFSBucket(database);

                //var test = fs.DownloadAsBytesByName(imeSlike);
                
                
                byte[] data;
                MemoryStream stream = new MemoryStream();


                slika.Save(stream, slika.RawFormat);
                data = stream.ToArray();

                //string host = "localhost";
                //int port = 27017;
                //string databaseName = "docs";

                //var _client = new MongoClient();
                // var _database = (MongoDatabase)_client.GetDatabase("docs");

                var client = new MongoClient("mongodb://localhost");
                var database = client.GetDatabase("docs");
                var fs = new GridFSBucket(database);
                GridFSUploadOptions opcije = new GridFSUploadOptions();

                opcije.ContentType = "image/"+format;
                opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length) / 4;

                fs.UploadFromBytes(imeSlike, data, opcije);

                //var grid = new MongoGridFS(new MongoServer(new MongoServerSettings { Server = new MongoServerAddress(host, port) }), databaseName, new MongoGridFSSettings());

               // grid.Upload(m, imeSlike, new MongoGridFSCreateOptions
                //{
                    //Id = 1,
               //     ContentType = "image/"+format
               // });

                return true;
            }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
               return false;
           }
        }