Save PDF File As Byte Array In to MongoDb Using c# ?
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));
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)
{
}
}
The method you use save As Byte Array In to MongoDb Using c# is really great. I hope you can share more pdf operations, such as writing, extracting, C# pdf to image and so on.
ReplyDeleteCS1503: Argument 1: cannot convert from 'MongoDB.Driver.WriteConcernResult' to 'string'
ReplyDeleteI'm getting this error while executing the same, can anyone help me out !!