hariom sharma

hariom sharma

  • 1.6k
  • 98
  • 13k

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

Aug 23 2017 2:12 AM

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)

{

}

}


Answers (1)