How to get the filename of the uploaded file

6,027 views
Skip to first unread message

Nadal

unread,
Feb 27, 2011, 1:12:13 AM2/27/11
to carrierwave
Here is my code


class Attachment < ActiveRecord::Base
mount_uploader :document, AttachmentUploader
end


attachment_uploader.rb is standard file. It added only following three
lines.


version :thumb do
process :resize_to_limit => [100, 100]
end


Now let's say that I uploaded "About Stacks.pdf" that comes with
mac.

I am trying to get the name of the uploaded file.


a = Attachment.last
a.document.filename #=> nil

a.document
=> #<AttachmentUploader:0x107160f78 @versions={:thumb=>#<#<Class:
0x1072146e0>:0x10715f7e0 @versions={}, @model=#<Attachment id: 26,
attachable_id: 76, attachable_type: "Comment", created_at: "2011-02-27
05:30:15", updated_at: "2011-02-27 05:30:15", document:
"about_stacks.pdf">, @file=#<CarrierWave::SanitizedFile:0x10715f2b8
@file="/Users/nsingh/dev/personal/demo_app/public/uploads/attachment/
document/26/thumb_about_stacks.pdf", @content_type=nil,
@original_filename=nil>, @storage=#<CarrierWave::Storage::File:
0x10715f6c8 @uploader=#<#<Class:0x1072146e0>:0x10715f7e0 ...>>,
@mounted_as=:document>}, @model=#<Attachment id: 26, attachable_id:
76, attachable_type: "Comment", created_at: "2011-02-27 05:30:15",
updated_at: "2011-02-27 05:30:15", document: "about_stacks.pdf">,
@file=#<CarrierWave::SanitizedFile:0x10715f998 @file="/Users/nadal/dev/
personal/demo_app/public/uploads/attachment/document/26/
about_stacks.pdf", @content_type=nil, @original_filename=nil>,
@storage=#<CarrierWave::Storage::File:0x10715fd08
@uploader=#<AttachmentUploader:0x107160f78 ...>>,
@mounted_as=:document>

I can see following code in attachment_uploader.rb


# Override the filename of the uploaded files:
# def filename
# "something.jpg" if original_filename
# end


I don't have any need to change the name or anything like that. So I
left that code as commented.

Again how do I get the name of the file that was uploaded?

Brandon Martin

unread,
Feb 27, 2011, 12:13:34 PM2/27/11
to carri...@googlegroups.com
Nadal

I believe what you want is 

a.document_filename



--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.




--


Nadal

unread,
Feb 27, 2011, 7:21:26 PM2/27/11
to carrierwave
>Attachment.last.document_filename
Attachment Load (0.5ms) SELECT `attachments`.* FROM `attachments`
ORDER BY attachments.id DESC LIMIT 1
NoMethodError: undefined method `document_filename' for #<Attachment:
0x107189b58>
> <http://zyphmartin.com>

Nadal

unread,
Mar 2, 2011, 8:45:21 AM3/2/11
to carrierwave
This solution worked for me just incase someone else runs into same
issue.

> Attachment.last.document.instance_variable_get('@file').filename
"about_stacks.pdf"

Brandon Martin

unread,
Mar 2, 2011, 9:05:07 AM3/2/11
to carri...@googlegroups.com

The solution I posted works fine for me on 4 different applications. Not sure why it didn't work for you.

Jonas Nicklas

unread,
Mar 2, 2011, 11:32:34 AM3/2/11
to carri...@googlegroups.com, Nadal
document_filename will only work on mongoid IIRC. This is actually
kind of an oversight, since there is no good API to do this. Ran into
this a couple of ways, and there is an easy way to cheat around this:

def document_filename
read_attribute(:document)
end

should do the trick, now you can call that method from the view. I'm
starting to believe that CarrierWave should by default map to a
different column, like Paperclip does, obviously this bis quite a
breaking change, so we probably can't change it. Just food for
thought. At least we should have a somewhat sensible API for this.

/Jonas

Trevor Turk

unread,
Mar 2, 2011, 4:35:05 PM3/2/11
to carri...@googlegroups.com, Nadal
On Wednesday, March 2, 2011 4:32:34 PM UTC, jnicklas wrote:

At least we should have a somewhat sensible API for this.

One simple idea that might work for all ORMs would be something along these lines:

def filename
  File.basename(path)
end

I haven't tested it, but I think something like that would work. 

Any opinions?

- Trevor

James Miller

unread,
Mar 15, 2011, 12:04:29 PM3/15/11
to carri...@googlegroups.com, Nadal
File.basename(path) works for getting the filename, but it seems to mess up the file naming for versions.  For some reason it then stores versions as thumb_thumb_filename.jpg instead of just thumb_filename.jpg (I'm using :fog storage).

Trevor Turk

unread,
Mar 16, 2011, 4:42:34 AM3/16/11
to carri...@googlegroups.com, Nadal
On Tuesday, March 15, 2011 4:04:29 PM UTC, James Miller wrote:
File.basename(path) works for getting the filename, but it seems to mess up the file naming for versions.  For some reason it then stores versions as thumb_thumb_filename.jpg instead of just thumb_filename.jpg (I'm using :fog storage).

Maybe you can calculate it based on the last part of the URL instead? That should be correct. 

There's an open issue about this on GitHub -- I think we should address it in Carrierwave itself. I'll work on it sometime in the future, but any help (pull requests with tests) would be most welcome. 

- Trevor 

Ankur

unread,
Apr 24, 2011, 10:13:47 PM4/24/11
to carrierwave
This works from the model containing the attachment, but how can we
get the filename from the instance of the attachment?
> > a.document.filename#=> nil

Ankur

unread,
Apr 25, 2011, 11:03:44 AM4/25/11
to carrierwave
It's YAMP, yet another monkey patch

I can submit this as a pull request if anyone likes it

class CarrierWave::Uploader::Base
def uploaded_filename
model.read_attribute mounted_as
end
end

Trevor Turk

unread,
Apr 26, 2011, 9:03:48 AM4/26/11
to carri...@googlegroups.com
If you feel like adding tests for all supported ORMs etc, please do!

Trevor Turk

unread,
May 21, 2011, 10:34:53 PM5/21/11
to carri...@googlegroups.com
FYI there's a feature about this in the master branch now: https://github.com/jnicklas/carrierwave/commit/0ac7af9a1bf458b344e23f75a861e88aa45b8485

Mike Fulcher

unread,
Jun 15, 2011, 7:23:12 AM6/15/11
to carrierwave
In ActiveRecord, can you not just call a[:document]? Carrierwave saves
the filename in the document column within the DB, so you can retrieve
this to get the filename.

On May 22, 3:34 am, Trevor Turk <trevort...@gmail.com> wrote:
> FYI there's a feature about this in the master branch
> now:https://github.com/jnicklas/carrierwave/commit/0ac7af9a1bf458b344e23f...
Reply all
Reply to author
Forward
0 new messages