public async Task <ObjectId> UploadFileAsync(Stream sourceStream, string fileName, GridFSUploadOptions uploadOptions = null)
        {
            var gridFsBucket = new GridFSBucket(this.database);
            GridFSUploadStream destinationStream = await gridFsBucket.OpenUploadStreamAsync(fileName, uploadOptions);

            //一次缓冲区大小
            long filesize = sourceStream.Length;
            int  maxlenth = 4096;

            byte[] buffer = new byte[maxlenth];
            while (filesize > 0)
            {
                if (filesize < maxlenth)
                {
                    maxlenth = (int)filesize;
                }
                await sourceStream.ReadAsync(buffer, 0, maxlenth);

                await destinationStream.WriteAsync(buffer, 0, maxlenth);

                filesize -= maxlenth;
            }

            await destinationStream.CloseAsync();

            return(destinationStream.Id);
        }
Esempio n. 2
0
        public async Task UploadFileAsync(
            IMongoDatabase database,
            string filePath,
            string fileName,
            GridFSUploadOptions uploadOptions = null)
        {
            var gridFsBucket = new GridFSBucket(database);

            using (FileStream sourceStream = File.Open(filePath, FileMode.Open))
            {
                using (
                    GridFSUploadStream destinationStream =
                        await gridFsBucket.OpenUploadStreamAsync(fileName, uploadOptions))
                {
                    await sourceStream.CopyToAsync(destinationStream);

                    await destinationStream.CloseAsync();
                }
            }
        }
Esempio n. 3
0
 public MongoWriteContentProvider(GridFSBucket bucket, string label)
 {
     this.stream = bucket.OpenUploadStream(label);
 }