2011-02-14 77 views
60

我正在開發一個以mongodb作爲我的後端的web應用程序。我希望讓用戶將圖片上傳到他們的個人資料中,例如鏈接的個人資料照片。我正在使用帶有MVC2的aspx頁面,並且我讀到GridFs庫用於將大型文件類型存儲爲二進制文件。我已經到處尋找線索了,但是mongodb沒有任何關於C#api或GridFs C#的文檔。我感到困惑和困惑,真的可以使用另一套大腦。MongoDB GridFs與C#,如何存儲文件,如圖像?

任何人都知道如何實際實現一個文件上傳控制器,它將用戶上傳的圖像存儲到一個mongodb集合中?太感謝了!

我試過這種變化無濟於事。

Database db = mongo.getDB("Blog"); 
GridFile file = new GridFile(db); 
file.Create("image.jpg"); 

var images = db.GetCollection("images"); 
images.Insert(file.ToDocument()); 
+0

HTTPS一個diff採取:// stackoverflow.com/questions/45535296/how-to-create-a-cdn-server-in-dotnet-core-using-mongodb-gridfs-and-angularjs?noredirect=1#comment78034138_45535296 – 2017-08-07 02:24:24

回答

84

下面的例子說明如何保存文件並從GridFS的(使用官方的MongoDB驅動程序)回讀:

var server = MongoServer.Create("mongodb://localhost:27020"); 
var database = server.GetDatabase("tesdb"); 

var fileName = "D:\\Untitled.png"; 
var newFileName = "D:\\new_Untitled.png"; 
using (var fs = new FileStream(fileName, FileMode.Open)) 
{ 
    var gridFsInfo = database.GridFS.Upload(fs, fileName); 
    var fileId = gridFsInfo.Id; 

    ObjectId oid= new ObjectId(fileId); 
    var file = database.GridFS.FindOne(Query.EQ("_id", oid)); 

    using (var stream = file.OpenRead()) 
    { 
     var bytes = new byte[stream.Length]; 
     stream.Read(bytes, 0, (int)stream.Length); 
     using(var newFs = new FileStream(newFileName, FileMode.Create)) 
     { 
     newFs.Write(bytes, 0, bytes.Length); 
     } 
    } 
} 

結果:

文件:

File im mongodb

塊收藏:

Chunks collection

希望這有助於。

+1

Andrew,你有什麼用來查看結果?截圖來自哪些程序?看起來很精簡的SQL管理器。 – Goff 2011-11-08 17:42:29

+8

@Goff:這是mongovue - > http://www.mongovue.com/。 – 2011-11-08 18:29:45

17

這個例子可以讓你綁一個文檔對象

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using MongoDB.Driver; 
using MongoDB.Driver.Linq; 
using MongoDB.Bson; 
using MongoDB.Driver.Builders; 
using MongoDB.Driver.GridFS; 
using System.IO; 

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     MongoServer ms = MongoServer.Create(); 
     string _dbName = "docs"; 

     MongoDatabase md = ms.GetDatabase(_dbName); 
     if (!md.CollectionExists(_dbName)) 
     { 
      md.CreateCollection(_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); 
      Console.WriteLine("Downloaded {0}", item.DocName); 
      Console.WriteLine("DocName {0} dowloaded", item.DocName); 
     } 

     Console.ReadKey(); 
    } 
} 

class Doc 
{ 
    public ObjectId Id { get; set; } 
    public string DocName { get; set; } 
    public ObjectId DocId { get; set; } 
} 
14

以上的答案是很快就現在的2.1 RC-0驅動程序已經發布已經過時。

在2.1 MongoDB的GridFS的與文件的工作方式,現在可以做到這樣:

using MongoDB.Bson; 
using MongoDB.Driver; 
using MongoDB.Driver.GridFS; 
using System.IO; 
using System.Threading.Tasks; 

namespace MongoGridFSTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var client = new MongoClient("mongodb://localhost"); 
      var database = client.GetDatabase("TestDB"); 
      var fs = new GridFSBucket(database); 

      var id = UploadFile(fs); 

      DownloadFile(fs, id); 
     } 

     private static ObjectId UploadFile(GridFSBucket fs) 
     { 
      using (var s = File.OpenRead(@"c:\temp\test.txt")) 
      { 
       var t = Task.Run<ObjectId>(() => { return 
        fs.UploadFromStreamAsync("test.txt", s); 
       }); 

       return t.Result; 
      } 
     } 

     private static void DownloadFile(GridFSBucket fs, ObjectId id) 
     { 
      //This works 
      var t = fs.DownloadAsBytesByNameAsync("test.txt"); 
      Task.WaitAll(t); 
      var bytes = t.Result; 


      //This blows chunks (I think it's a driver bug, I'm using 2.1 RC-0) 
      var x = fs.DownloadAsBytesAsync(id); 
      Task.WaitAll(x); 
     } 
    } 
} 

這是從C#的驅動程序測試here

相關問題