How to retrieve file content(.txt , .pdf etc .,) from mongodb gridfs using carrierwave , mongoid grid fs?

872 Views Asked by At

I am getting issues with gridfs file system when try to retrieve files from gridfs, I could able to store files(text,pdf) using carrierwave mongoid gridfs and unable to get the data from the mongodb , Can anyone please help me

I uploaded sample text file into gridfs , it stored the file content in database please look at the following record where you can see the actual record from database

#<Email _id: 547f17f37261740ea9000000, from: "[email protected]", to: "[email protected]", 
  cc: nil, bcc: nil, subject: "testmail", body: "h1. Give RedCloth a try! A *simple*
  paragraph with a line break , some_emphasis_and a \"link\":http://redcloth.org", 
  attachment1: #<BSON::Binary:0xe129374 @data="this is a simple file a couple of lines
  long\nsecond line\nthird line", @type=:generic>, attachment2: nil, attachment3: nil>

see the field attachment1 it show you the gridfs content and @data stores my actual file content , upto this all is fine , but when I try to get the data from this record I am unable to get it , I tried like the following

@email.attachment1.read   ==> shows me nil value
@email.attachment1.file.read => also shows me nil value
also tried @email.instance_variable_get(:@data) ==> not worked 

How to fix this , please help me . Thanks in advance .

0

There are 0 best solutions below