How to display the file name of an upload in Ruby on Rails

Posted on 04 January 2007

Often you want to display just the filename of an upload/attachment that a user has uploaded. Just do the following:

File.basename(@vacancy.document)

The path will be stripped out and you are left with just the filename and extension.

If you don't want the extension (suffix) to be output you could do:

File.basename(@vacancy.document, File.extname(@vacancy.document))

Basically if you pass in the extension then it will not be returned.

File.extname just returns the extension (the portion of file name in path after the period)

I often combine this with File.size to show the user both the format of the upload and the file size

Got something to say?