当前位置: 首页>>代码示例>>C#>>正文


C# MongoGridFS.Upload方法代码示例

本文整理汇总了C#中MongoDB.Driver.GridFS.MongoGridFS.Upload方法的典型用法代码示例。如果您正苦于以下问题:C# MongoGridFS.Upload方法的具体用法?C# MongoGridFS.Upload怎么用?C# MongoGridFS.Upload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoDB.Driver.GridFS.MongoGridFS的用法示例。


在下文中一共展示了MongoGridFS.Upload方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Upload

 /// <summary>
 /// Store a file in the database
 /// </summary>
 /// <param name="stream">The stream of the files content</param>
 /// <param name="fileName">The remote filename</param>
 /// <param name="contentType">The file's content type</param>
 /// <returns>GridFS File Info</returns>
 public MongoGridFSFileInfo Upload(Stream stream, string fileName, string contentType)
 {
     MongoGridFS fs = new MongoGridFS(this.store);
     MongoGridFSCreateOptions options = new MongoGridFSCreateOptions();
     options.ContentType = contentType;
     return fs.Upload(stream, fileName, options);
 }
开发者ID:gazeth,项目名称:Bullet,代码行数:14,代码来源:GridFS.cs

示例2: Add

        /// <summary>
        /// 添加本地文件
        /// </summary>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="remoteFile">服务Id</param>
        /// <returns></returns>
        public MetaInfo Add(string filePath, string remoteFile)
        {
            try
            {
                _logger.DebugFormat("Add File filePath:{0}, remoteId:{1}", filePath, remoteFile);

                MongoGridFSCreateOptions option = new MongoGridFSCreateOptions
                {
                    Id = remoteFile,
                    UploadDate = DateTime.Now,
                    ContentType = MimeMapper.GetMimeMapping(filePath),
                };

                using (var stream = new FileStream(filePath, FileMode.Open))
                {
                    MongoGridFS fs = new MongoGridFS(_context.DataBase);

                    var info = fs.Upload(stream, remoteFile, option);
                    return new MetaInfo
                    {
                        fileName = remoteFile,
                        MD5 = info.MD5,
                        MimeType = info.ContentType,
                    };
                }

            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                throw;
            }
        }
开发者ID:ideayapai,项目名称:docviewer,代码行数:40,代码来源:MongoPolicy.cs

示例3: UploadImageToDish

        public static MongoGridFSFileInfo UploadImageToDish(this Dish dishWithImages, MongoGridFS gridFS, System.IO.Stream fs, string imageName, string contentType)
        {
            log.DebugFormat("[UploadImageToDish] This RestaurantBasicData.Id={0}, MongoGridFS, imageName={1}, contentType={2}.", dishWithImages.Id, gridFS.ToString(), imageName, contentType);

            MongoGridFSCreateOptions gridFSOption = new MongoGridFSCreateOptions();
            gridFSOption.ContentType = contentType;
            var gridFsInfo = gridFS.Upload(fs, imageName, gridFSOption);
            ImageData convertedValue = ImageServices.ConvertToImageData(gridFsInfo);
            dishWithImages.Image = convertedValue;
            //dishWithImages.Images[0] = convertedValue;
            var fileId = gridFsInfo.Id;
            return gridFsInfo;
        }
开发者ID:pashkov,项目名称:Spontaneous,代码行数:13,代码来源:ImageServices.cs

示例4: UploadImageToDish

        public static MongoGridFSFileInfo UploadImageToDish(this Dish dishWithImages, MongoGridFS gridFS, System.IO.Stream fs, string imageName)
        {
            //resize picture 100 * 100
            //save to profile
            //on output implement logic of presentation  - if we have image let's display it
            //copy all images that we have on links to gridFS
            //Upload images from backoffice.

            log.DebugFormat("[UploadImageToDish] This RestaurantBasicData.Id={0}, MongoGridFS, imageName={1}.", dishWithImages.Id, gridFS.ToString(), imageName);

            var gridFsInfo = gridFS.Upload(fs, imageName);
            ImageData convertedValue = ImageServices.ConvertToImageData(gridFsInfo);
            dishWithImages.Image = convertedValue;
            var fileId = gridFsInfo.Id;
            return gridFsInfo;
        }
开发者ID:vaduha,项目名称:Spontaneous,代码行数:16,代码来源:ImageServices.cs

示例5: UploadFile

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public UploadResult UploadFile(HttpContextBase context,string collection)
        {
            var client = MongoDbServer.MongoDbClient;
            var server = client.GetServer();
            HttpPostedFileBase file = context.Request.Files[0];
            UploadResult Result = new UploadResult() { State = UploadState.Unknown };
            var Size = file.ContentLength * 1.0 / 1024 / 1024;
            var extName = System.IO.Path.GetExtension(file.FileName).ToLower();
            Result.Url = FileDomain;
            Result.OriginFileName=file.FileName;
            if (CustomConfig.FileExts.Contains(extName))
            {
                Random rd = new Random();
                var fileId = DateTime.Now.Ticks + rd.Next(9999) + extName;
                if (Size < CustomConfig.CanUploadFileSize)
                {
                    if (Size <= 6)
                    {
                        SunFile sunfile = new SunFile();
                        byte[] data = new byte[file.ContentLength];
                        file.InputStream.Read(data, 0, data.Length);
                        sunfile.Id = fileId;
                        sunfile.Data = data;
                        sunfile.Time = DateTime.Now;
                        var imgCollection = server.GetDatabase(fileDatabase).GetCollection<SunFile>(collection);
                        imgCollection.Insert(sunfile);

                        Result.Url += "/files/SmallFile/" + client.DbGroupName + "/" + collection +"/"+ fileId;
                        Result.State = UploadState.Success;
                    }
                    else
                    {
                        MongoGridFS gfs = new MongoGridFS(server, fileDatabase, new MongoGridFSSettings());
                        gfs.Upload(file.InputStream, fileId);
                        Result.Url += "/files/LargeFile/" + client.DbGroupName + "/" + fileId;
                        Result.State = UploadState.Success;
                    }
                    Result.FileId = fileId;
                }
                else
                {
                    Result.State = UploadState.SizeLimitExceed;
                }
            }
            else { Result.State = UploadState.TypeNotAllow; }
            return Result;
        }
开发者ID:wangscript,项目名称:sunmvc,代码行数:52,代码来源:MUpload.cs

示例6: AddStream

        /// <summary>
        /// 流模式添加文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="remoteFile"></param>
        /// <returns></returns>
        public MetaInfo AddStream(Stream stream, string mimetype, string remoteFile)
        {
            _logger.DebugFormat("Add File By Stream, Id {0}.",remoteFile);

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

                MongoGridFS fs = new MongoGridFS(_context.DataBase);

                var info = fs.Upload(stream, remoteFile, option);
                return new MetaInfo
                {
                    fileName = remoteFile,
                    MD5 = info.MD5,
                    MimeType = info.ContentType,
                };

            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                throw;
            }
        }
开发者ID:ideayapai,项目名称:docviewer,代码行数:37,代码来源:MongoPolicy.cs

示例7: TestUploadArrayFile2GridFS

        public void TestUploadArrayFile2GridFS()
        {
            var server = MongoServer.Create("mongodb://localhost:27017");
            var db = server.GetDatabase("dcm");

            FileStream fs = new FileStream(@"D:\Download\FileTestUpload\Hello2.txt", FileMode.Open, FileAccess.Read);
            System.IO.Stream strm = fs;

            List<byte> readByte = new List<byte>();
            int bufferSize = 4048;
            byte[] buffer = new byte[bufferSize];
            int readCount = strm.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                readByte.AddRange(buffer.Take(readCount).ToArray());
                readCount = strm.Read(buffer, 0, bufferSize);
            }
            var micBytes = readByte.ToArray();

            Stream stream = new MemoryStream(micBytes);

            MongoGridFS file = new MongoGridFS(db);
            var upFile = file.Upload(stream, "ArrayHi");
            Console.WriteLine("MD5: {0}", upFile.MD5);
        }
开发者ID:chaiwat-kh,项目名称:Lab-MongoDB,代码行数:25,代码来源:TestGridFS.cs

示例8: TestUploadStreamFile2GridFS

        public void TestUploadStreamFile2GridFS()
        {
            var server = MongoServer.Create("mongodb://localhost:27017");
            var db = server.GetDatabase("dcm");

            FileStream fs = new FileStream(@"D:\Download\FileTestUpload\Hello2.txt", FileMode.Open, FileAccess.Read);
            System.IO.Stream strm = fs;

            MongoGridFS file = new MongoGridFS(db);
            MongoGridFSCreateOptions op = new MongoGridFSCreateOptions();
            op.Aliases = new string[]{"Home"};

            var upFile = file.Upload(strm, "SayHi", op);
            Console.WriteLine("MD5: {0}", upFile.MD5);
        }
开发者ID:chaiwat-kh,项目名称:Lab-MongoDB,代码行数:15,代码来源:TestGridFS.cs

示例9: Test2

        static void Test2()
        {
            MongoServer ms = MongoServer.Create();
            string _dbName = "docs";

            MongoDatabase md = ms.GetDatabase(_dbName);
            if (!md.CollectionExists(_dbName))
            {
                md.CreateCollection(_dbName);
            } // End if (!md.CollectionExists(_dbName))

            MongoCollection<Doc> _documents = md.GetCollection<Doc>(_dbName);
            _documents.RemoveAll();
            //add file to GridFS

            MongoGridFS gfs = new MongoGridFS(md);
            MongoGridFSFileInfo gfsi = gfs.Upload(@"c:\mongodb.rtf");
            _documents.Insert(new Doc()
                {
                    DocId = gfsi.Id.AsObjectId,
                    DocName = @"c:\foo.rtf"
                }
            );

            foreach (Doc item in _documents.FindAll())
            {

                ObjectId _documentid = new ObjectId(item.DocId.ToString());
                MongoGridFSFileInfo _fileInfo = md.GridFS.FindOne(Query.EQ("_id", _documentid));
                gfs.Download(item.DocName, _fileInfo);
                System.Console.WriteLine("Downloaded {0}", item.DocName);
                System.Console.WriteLine("DocName {0} dowloaded", item.DocName);
            } // Next item

            //System.Console.ReadKey();
        }
开发者ID:moacap,项目名称:MongoUpload,代码行数:36,代码来源:MongoInit.cs


注:本文中的MongoDB.Driver.GridFS.MongoGridFS.Upload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。