Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Problems with NoRM and GridFS
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Tsombakos  
View profile  
 More options Sep 14 2011, 3:24 pm
From: John Tsombakos <johnt...@gmail.com>
Date: Wed, 14 Sep 2011 12:24:08 -0700 (PDT)
Local: Wed, Sep 14 2011 3:24 pm
Subject: Problems with NoRM and GridFS

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »