Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:51:21+00:00 2026-06-06T14:51:21+00:00

I’m using perl and MongoDB::GridFS (along with other mongo modules) to store and retrieve

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:51:22+00:00Added an answer on June 6, 2026 at 2:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.