I'm trying to use the GridFS capability of the NoRM driver, but have run
into a strange problem.
I have code (an ASP.NET site), that when a file is selected, the file is
uploaded and put into a Mondo database, using NoRM and GridFS. The first
problem where the file wasn't actually stored in the database was fixed by
the post:
https://groups.google.com/d/msg/norm-mongodb/nzLXGyYwNnA/Ylf9oz-8ogkJ
The upload code is:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// opens the database and will create the new database
if it does not exist
using (var server =
Mongo.Create("mongodb://server/TestDB"))
{
// gets your class object collection
var collection = server.GetCollection<Record>();
Record newRecord = new Record();
var fs = FileUpload1.FileBytes; // gets the file
your uploading
var file = new GridFile(); // creates a new
mongo file object
var fileCollection = server.Database.Files();
//gets the collection of files in the database
// set the file properties
file.FileName = FileUpload1.FileName;
file.Content= fs; // the content of the file
file.ContentType = "application/pdf";
fileCollection.Save(file); // uploads to the
database
newRecord.file_id = file.Id; // gets the id of
the file that was just uploaded
// fill in the rest of the class proprties
newRecord.filename = FileUpload1.FileName;
newRecord.introDesc = txtIntro.Text;
newRecord.category = "category1";
newRecord.category2 = "category2";
newRecord.category3 = "category3";
newRecord.dateAdded = DateTime.Today;
newRecord.status = "A";
newRecord.paid = "yes";
newRecord.teaser = txtTeaser.Text;
newRecord.title = FileUpload1.FileName;
collection.Save(newRecord);
txtIntro.Text = "";
txtTeaser.Text = "";
}
lblMsg.Text = "File was uploaded";
}
}
That seems to work fine, I see the entry in the database, both the files
collection and the chunks. Now I have code that, given some criteria, will
retrieve the file and stream it to the web page:
public void showPDF()
{
using (var server = Mongo.Create("mongodb://server/TestDB"))
{
var collection = server.GetCollection<Record>();
var q = (from r in collection.AsQueryable()
where r.filename == pdfFile
select r).FirstOrDefault();
var gridFS = server.Database.Files();// gets the files
collection
//var file = gridFS.FindOne(new{ filename= pdfFile});
var file = gridFS.FindOne(new { _id = q.file_id });
var contents = file.Content.ToArray();
Response.Clear();
Response.AddHeader("Content-Length",
contents.Length.ToString());
Response.ContentType = file.ContentType;
Response.AppendHeader("Content-Disposition", "attachment;
filename= " + pdfFile);
// Write the memory stream containing the file directly to
//the Response object that gets sent to the client
Response.OutputStream.Write(contents, 0,
Convert.ToInt32(contents.Length));
// Must end response because the rest of the pages html code
will
//add to the file that is trying to open
Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
And that works, the PDF will show/be download... the first time. From there
after, even though the file is in the database, examining the file.Content
shows the size is 0
Has anyone used the GridFS and run into this problem, the second and
subsequent queries return an empty content?
Thanks.