Sunday, 15 February 2015

mongodb - Why does sending files from GridFS via MVC4 take so much time? -



mongodb - Why does sending files from GridFS via MVC4 take so much time? -

i want send images stored in mongodb using gridfs via mvc4 web app browser via lan environment, take ~500ms until image sent browser.

google chrome network inspector says of time spent during "waiting" while actual "receiving" takes ~1ms.

the mongodb server in local network, can take long send 10kb image? utilize windows 8 visual studio 2012 , official mongo-csharp-driver via nuget.

here code of "files" controller takes object id , sends info id:

public filecontentresult files(string id) { var database = new mongoclient(myconnection).getserver().getdatabase("mydb"); var gridfs = new mongogridfs(database); var bsonid = new bsonobjectid(id); var gridinfo = gridfs.findonebyid(bsonid); var bytes = gridinfotoarray(gridinfo); homecoming new filecontentresult(bytes, "image/jpeg") { filedownloadname = gridinfo.name }; } private byte[] gridinfotoarray(mongogridfsfileinfo file) { using (var stream = file.openread()) { var bytes = new byte[stream.length]; stream.read(bytes, 0, (int)stream.length); homecoming bytes; } }

code display image in view:

<img src="@url.action("files", new { id = objectidofmyimage) })"/>

how different results if cache database , mongogridfs instances?

// create static fields _database & _gridfs var database = _database ?? (_database = new mongoclient(myconnection).getserver().getdatabase("mydb")); var gridfs = _gridfs ?? (_gridfs = new mongogridfs(database));

i'm not sure how much overhead incurs when instantiate these, wouldn't wound move outside of method you're trying optimize.

mongodb asp.net-mvc-4 mongodb-csharp iis-8 gridfs

No comments:

Post a Comment