OpenWrite() публичный Метод

Opens an existing GridFS file for writing.
public OpenWrite ( string remoteFileName ) : MongoGridFSStream
remoteFileName string The remote file name.
Результат MongoGridFSStream
Пример #1
0
        public void AttachFile(byte[] data, FileReference fileData, SCORMObject obj)
        {
            List<FileReference> files = new List<FileReference>();
            if (obj.Files != null)
            {
                files.AddRange(obj.Files);
            }

            var db = MongoDB.Driver.MongoDatabase.Create(_ConnectionString);
            var gfs = new MongoGridFS(db);
            var id = Guid.NewGuid().ToString();
            using (var file = gfs.OpenWrite(id))
            {
                file.Write(data, 0, data.Length);
                file.Flush();
            }
            var fileInfo = gfs.FindOne(id);
            gfs.SetContentType(fileInfo, fileData.ContentType);
            fileData.FileId = id;
            files.Add(fileData);
            obj.Files = files;
        }
Пример #2
0
        public Stream OpenStream(string remoteFile, string mimetype)
        {
            _logger.DebugFormat("Open MongoDB By Stream, Id {0}.", remoteFile);

            Stream stream = null;

            try
            {
                MongoGridFSCreateOptions option = new MongoGridFSCreateOptions
                {
                    Id = remoteFile,
                    UploadDate = DateTime.Now,
                    ContentType = mimetype,
                };

                MongoGridFS fs = new MongoGridFS(_context.DataBase);
                stream = fs.OpenWrite(remoteFile, option);
                return stream;
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                if (stream != null)
                {
                    stream.Close();
                }
                throw;
            }
        }