How to save uploaded images to a MongoDB collection, and retrieve it - OmIndia

Teach To India

Monday, August 28, 2017

How to save uploaded images to a MongoDB collection, and retrieve it

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 { 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));
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; }
}
protected void btn_download_Click(object sender, EventArgs e)
{
}
}


2 comments:

  1. 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.

    ReplyDelete
  2. CS1503: Argument 1: cannot convert from 'MongoDB.Driver.WriteConcernResult' to 'string'
    I'm getting this error while executing the same, can anyone help me out !!

    ReplyDelete

Comments

Popular