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