public partial class Default2 : System.Web.UI.Page
{
public class DataBaseName
{
public string Database { get; set; }
public string CollectionName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string name=FileUpload1.PostedFile.FileName;
string path=@"~\PDFFILE\"+name;
FileUpload1.PostedFile.SaveAs(Server.MapPath(path));
//------------------------------------------------//
var con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
MongoClient Client_ = new MongoClient(con);
MongoServer Server_ = Client_.GetServer();
DataBaseName dname = new DataBaseName();
dname.Database = "Book";
dname.CollectionName = "PDFDATA";
MongoDatabase db = Server_.GetDatabase(dname.Database);
// database connection string and client and servr connect with database of mongo db //
MongoCollection<Doc> col = db.GetCollection<Doc>(dname.CollectionName);
MongoGridFS mgfs = new MongoGridFS(db);
MongoGridFSFileInfo mgfsinfo = mgfs.Upload(Server.MapPath(path));
mgfs.Upload(
mgfsinfo=mgfs.Upload(
col.Insert(new Doc
{
DocId=mgfsinfo.Id.AsObjectId,
DocName=mgfsinfo.Name
});
}
}
class Doc
{
public ObjectId Id { get; set; }
public string DocName { get; set; }
public ObjectId DocId { get; set; }
}
protected void btn_download_Click(object sender, EventArgs e)
{
}
}

Gokhul VarmanPosted Aug 29, 2017, 9:04 AM
From a performance perspective, saving and retrieving the file in binary form is faster than first serializing it to a string. In a test program, running against a MongoDb 3.2 database, saving a file in binary form in a document was up to 3 times faster than saving the file in a string-serialized form. Which is understandable, since the string-serialized form is simply 'more bytes' to save or read.
In the same test program a quick test was also performed against GridFS, but there you really have to play a round with the chunck-size to get to the best possible performance.