How to get the filename of the uploaded file

6,026 ವೀಕ್ಷಣೆಗಳು
ಮೊದಲು ಓದದ ಸಂದೇಶಕ್ಕೆ ಸ್ಕಿಪ್ ಮಾಡಿ

Nadal

ಓದದಿರುವುದು,
ಫೆಬ್ರವರಿ 27, 2011, 01:12:13 ಪೂರ್ವಾಹ್ನ27/2/11
ಗೆ 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

ಓದದಿರುವುದು,
ಫೆಬ್ರವರಿ 27, 2011, 12:13:34 ಅಪರಾಹ್ನ27/2/11
ಗೆ 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

ಓದದಿರುವುದು,
ಫೆಬ್ರವರಿ 27, 2011, 07:21:26 ಅಪರಾಹ್ನ27/2/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 2, 2011, 08:45:21 ಪೂರ್ವಾಹ್ನ2/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 2, 2011, 09:05:07 ಪೂರ್ವಾಹ್ನ2/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 2, 2011, 11:32:34 ಪೂರ್ವಾಹ್ನ2/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 2, 2011, 04:35:05 ಅಪರಾಹ್ನ2/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 15, 2011, 12:04:29 ಅಪರಾಹ್ನ15/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಮಾರ್ಚ್ 16, 2011, 04:42:34 ಪೂರ್ವಾಹ್ನ16/3/11
ಗೆ 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

ಓದದಿರುವುದು,
ಏಪ್ರಿ 24, 2011, 10:13:47 ಅಪರಾಹ್ನ24/4/11
ಗೆ 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

ಓದದಿರುವುದು,
ಏಪ್ರಿ 25, 2011, 11:03:44 ಪೂರ್ವಾಹ್ನ25/4/11
ಗೆ 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

ಓದದಿರುವುದು,
ಏಪ್ರಿ 26, 2011, 09:03:48 ಪೂರ್ವಾಹ್ನ26/4/11
ಗೆ carri...@googlegroups.com
If you feel like adding tests for all supported ORMs etc, please do!

Trevor Turk

ಓದದಿರುವುದು,
ಮೇ 21, 2011, 10:34:53 ಅಪರಾಹ್ನ21/5/11
ಗೆ carri...@googlegroups.com
FYI there's a feature about this in the master branch now: https://github.com/jnicklas/carrierwave/commit/0ac7af9a1bf458b344e23f75a861e88aa45b8485

Mike Fulcher

ಓದದಿರುವುದು,
ಜೂನ್ 15, 2011, 07:23:12 ಪೂರ್ವಾಹ್ನ15/6/11
ಗೆ 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...
ಎಲ್ಲರಿಗೂ ಪ್ರತ್ಯುತ್ತರಿಸಿ
ಲೇಖಕರಿಗೆ ಪ್ರತ್ಯುತ್ತರಿಸಿ
ಫಾರ್ವರ್ಡ್ ಮಾಡಿ
0 ಹೊಸ ಸಂದೇಶಗಳು