How to save PDF Byte Array In to MongoDb Using c# ? - OmIndia

Teach To India

Wednesday, July 11, 2018

How to save PDF Byte Array In to MongoDb Using c# ?

How to save PDF Byte Array In to MongoDb Using c# ?


public partial class Default2 : System.Web.UI.Page
{
    public class DataBaseName 
    {
      public string Database { getset; }
      public string CollectionName { getset; }
     }
    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 { getset; }
   public string DocName { getset; }
   public ObjectId DocId { getset; }
  }

}

No comments:

Post a Comment

Comments

Popular