[MongoDB]GridFS如何取得上傳的檔案的明細How to get uploaded file's GridFSFileInfo

  • 1508
  • 0
  • 2015-11-23

摘要:[MongoDB]GridFS如何取得上傳的檔案的明細How to get uploaded file's GridFSFileInfo

c# driver version:2.1:

用web form專案作測試,直接看範例即可囉:

.aspx


 *請輸入:ObjectID:<asp:textbox ID="objectIDTxt" runat="server" ></asp:textbox>
    <asp:Button ID="DownloadFromObjIDBtn" runat="server" Text="DownloadFromObjIDBtn" OnClick="DownloadFromObjIDBtn_Click" />

.aspx.cs


public partial class TestGetFileInfo : System.Web.UI.Page
{
	protected static IMongoClient _client;
	protected static IMongoDatabase _database;

	protected void Page_Load(object sender, EventArgs e)
	{
		//此兩行初始化MongoClient, 利用client存取資料庫,建議放在Constructor
		_client = new MongoClient("mongodb://localhost");
		_database = _client.GetDatabase("robot");

		ObjectIDTxt.Focus();
	}

	protected void GetFileInfoBtn_Click(object sender, EventArgs e)
	{
		
		if(ObjectIDTxt.Text.Trim() =="")
		{
			Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('請輸入ObjectID!')", true);
			return;
		}
		string objectID = ObjectIDTxt.Text.Trim();
		GridFSBucketOptions bucketOptions = new GridFSBucketOptions();
		bucketOptions.BucketName = "Alliance";

		ObjectId gridfsObjectID = new ObjectId(objectID);

		//下面這行寫法OK,是因為字串與字串可以直接比較
		//var filter = Builders.Filter.Eq(x => x.Filename, "9fdd3901-ee7d-430b-8cbe-21eff0661560.pdf");
		//無法正確執行,Unable to determine the serialization information for Id,是因為物件與物件無法直接做比較
		//var filter = Builders.Filter.Eq(x=>x.Id,gridfsObjectID);
		//參考這篇文章之後
		//http://stackoverflow.com/questions/33495208/mongodbfail-to-get-gridfsfileinfo-by-objectid-but-succeed-by-filename
		//Paul的解答有效!這個Eq的寫法會把_id隱性轉型(術語叫implicit convertion)為ObjectID型態
		var filter = Builders.Filter.Eq("_id", gridfsObjectID);
					
		var findOptions = new GridFSFindOptions();
		findOptions.Limit = 1;
		
		var myBucket = new GridFSBucket(_database, bucketOptions);
		
		//官網僅提供非同步方法範例
		//這是Miguel的解答,compile會有錯
		//using (var taskOfCursor = Task.Run(() => myBucket.FindAsync(x => x.Id == new MongoDB.Bson.ObjectId(objectID), findOptions)))
		using (var taskOfCursor = Task.Run(() => myBucket.FindAsync(filter, findOptions)))            
		{
			var test = taskOfCursor.Result;
			var taskOfList = Task.Run(() => taskOfCursor.Result.ToListAsync());
			GridFSFileInfo fileInfo = taskOfList.Result.FirstOrDefault();
			if (fileInfo != null)
			{
				FileNameLbl.Text = fileInfo.Filename;
			}
		}


	}

}

C# driver version 1.8.2的:

.aspx:


請輸入ObjectID以取得檔案名稱:<asp:TextBox ID="ObjectIDForFileNameText" runat="server"></asp:TextBox>
            <asp:Button ID="GetFileName" runat="server" Text="GetFileName" OnClick="GetFileNameBtn_Click" />
             <asp:Label ID="FileNameLbl" runat="server" ></asp:Label>

.aspx.cs


private static MongoClient client;
private static MongoServer server;
private static MongoDatabase database;
protected void Page_Load(object sender, EventArgs e)
{
	client = new MongoClient("mongodb://localhost");
	server = client.GetServer();
	database = server.GetDatabase("tesdb");
}
protected void GetFileNameBtn_Click(object sender, EventArgs e)
{
  
	ObjectId oid = new ObjectId(ObjectIDForFileNameText.Text.Trim());            
	//讀出來是個GridFSStream物件
	var file = database.GridFS.FindOne(Query.EQ("_id", oid));
	FileNameLbl.Text = file.Name;

}

參考文章:

Finding Files

http://mongodb.github.io/mongo-csharp-driver/2.1/reference/gridfs/findingfiles/

Mongodb:Fail to get GridFSFileInfo by ObjectID, but succeed by filename

http://stackoverflow.com/questions/33495208/mongodbfail-to-get-gridfsfileinfo-by-objectid-but-succeed-by-filename