Skip to content

Instantly share code, notes, and snippets.

@314pies
Created March 13, 2017 05:46
Show Gist options
  • Save 314pies/287585651823fce43dcd59b2736e6c4c to your computer and use it in GitHub Desktop.
Save 314pies/287585651823fce43dcd59b2736e6c4c to your computer and use it in GitHub Desktop.
TestingCodes
using UnityEngine;
using System.Collections;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using System.IO;
public class HiMongo : MonoBehaviour {
// Use this for initialization
void Start () {
var client = new MongoClient("mongodb://localhost:27017");
var server = client.GetServer();
var database = server.GetDatabase("tesdb");
var fileName = "F:\\Untitled2.png";
var newFileName = "F:\\new_Untitled2.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.ToString());
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);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment