1

I'm using perl and MongoDB::GridFS (along with other mongo modules) to store and retrieve files. It works fine with .txt, but I want to store and retrieve .docx. Here is my code:

#!usr/bin/perl

use MongoDB::GridFS;
use MongoDB;
use MongoDB::Database;
use MongoDB::OID;

my $conn = new MongoDB::Connection; 
my $db   = $conn->test; #name of our local db is test...default of mongoDB
my $coll = $db->err0; #err0 is the name of the collection
my $grid = $db->get_gridfs;
my $fh = IO::File->new("wordtoyamutha.docx", "r");
$grid->insert($fh, {"filename" => "test"});

my $outfile = IO::File->new("wordtoyamutha.docx", "w");
my $file = $grid->find_one({"filename" => "test"});;
$file->print($outfile);

I first created a .docx called "wordtoyamutha.docx", then ran the above code with the last three lines commented out. It ran well and a new entry appeared in my MongoDB fs.files. I then deleted the .docx ran the code with all the "storing" code commented out- to be explicit these lines were commented out from the above:

my $fh = IO::File->new("wordtoyamutha.docx", "r");
$grid->insert($fh, {"filename" => "test"});

A docx appeared with the title wordtoyamutha...but when i tried to open it Word complained that it was rendered unreadable via corruption.

I don't know of any other way to retrieve files...and this is all the perl MongoDB::GridFS suggests to do...what is the trick here?

The exact error from Word appears in a dailog and says "The file wordtoyamutha cannot be opened because there are problems with the contents".

4

1 回答 1

2

And here is the (fully functional!) results of the help I was given:

#!usr/bin/perl
#Using Strawberry Perl and on Windows 7 box

use strict;
use warnings;
use MongoDB::GridFS;
use MongoDB;
use MongoDB::Database;
use MongoDB::OID;

my $conn = new MongoDB::Connection; 
my $db   = $conn->test; #name of our local db is test...default of mongoDB

my $grid = $db->get_gridfs;
my $fh = IO::File->new("cool.docx", "r");
$fh->binmode();
$grid->insert($fh, {"filename" => "docx"});

my $outfile = IO::File->new("return.docx", "w");
$outfile->binmode();
my $file = $grid->find_one({"filename" => "docx"});
$file->print($outfile);

Again, this is not a great script - things are hardcoded here. This pulls a docx file titled "cool" from the same directory as the perl script, stores it in my db "test" and then retrieves it again in the same directory as the perl script as return.docx. AKA return.docx will appear in the directory and be an exact copy! This is not very useful alone - but obviously if we can do this it implies we can do much more.

=======WHAT DO I NEED TO CHANGE TO MAKE THIS SCRIPT WORK FOR ME?========

The parameter name "docx" is changeable - it is how the file will be addressed in the GridFS database. Then obviously the filename is changeable. Future users will probably have to change the "my $db" line as well to

my $db   = $conn->name_of_your_DB; 

If you don't know what the name of your DB is, go into the Mongo shell, type

show dbs

it gives a list of dbs. Now choose one of the listed dbs - we will call it db_you_want_to_use. Now type

use db_you_want_to_use

And change that $db line in the script to point to the same thing you just typed, AKA:

my $db   = $conn->db_you_want_to_use; 

Upon inserting a file with GridFS it creates a collection called fs.files. Use

db.fs.files.find()

To see the entries in your DB - AKA to check if things were properly inserted.

于 2012-06-22T15:01:25.930 回答