public Upload ( Stream stream, string remoteFileName ) : MongoDB.Driver.GridFS.MongoGridFSFileInfo | ||
stream | Stream | The source stream. |
remoteFileName | string | The remote file name. |
return | MongoDB.Driver.GridFS.MongoGridFSFileInfo |
/// <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; } }
/// <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); }
/// <summary> /// Copies a GridFS file. /// </summary> /// <param name="destFileName">The destination file name.</param> /// <param name="createOptions">The create options.</param> /// <returns>The file info of the new GridFS file.</returns> public MongoGridFSFileInfo CopyTo( string destFileName, MongoGridFSCreateOptions createOptions ) { // note: we are aware that the data is making a round trip from and back to the server // but we choose not to use a script to copy the data locally on the server // because that would lock the database for too long var stream = OpenRead(); return(gridFS.Upload(stream, destFileName, createOptions)); }
/// <summary> /// Copies a GridFS file. /// </summary> /// <param name="destFileName">The destination file name.</param> /// <param name="createOptions">The create options.</param> /// <returns>The file info of the new GridFS file.</returns> public MongoGridFSFileInfo CopyTo(string destFileName, MongoGridFSCreateOptions createOptions) { EnsureServerInstanceIsPrimary(); using (_server.RequestStart(_serverInstance)) { // note: we are aware that the data is making a round trip from and back to the server // but we choose not to use a script to copy the data locally on the server // because that would lock the database for too long var gridFS = new MongoGridFS(_server, _databaseName, _settings); var stream = OpenRead(); return(gridFS.Upload(stream, destFileName, createOptions)); } }
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; }
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; }
/// <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; }
/// <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; } }
public void TestUploadArrayFile2GridFS() { var server = MongoServer.Create("mongodb://*****:*****@"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); }
public void TestUploadStreamFile2GridFS() { var server = MongoServer.Create("mongodb://*****:*****@"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); }
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(); }