I am checking the file size of an uploaded file and storing it in a filesize
column. It will work just fine when the user uploads any files other than
images (jpg,png,gif).
Word, Excel, Powerpoint, .zip and more all work fine.
This is my model asset.rb that handles the file upload.
def newfile=(newfile_field)
self.filename = base_part_of(newfile_field.original_filename)
self.filetype = newfile_field.content_type.chomp
self.filesize = File.size(newfile_field) ### It fails on this line for
images...
end
The error I get is: "can''t convert StringIO into String"
I''m sure there''s an easier method/property to get the file
size of the
uploaded file. Like, reading the HTTP headers?
How do they get the file size in the validator?
validates_filesize_of :field, :in => 15.kilobytes..1.megabyte
Thanks for any leads,
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/e9214f59/attachment.html
An update...
If I move that File.size() logic out of the model and into the controller I
can get it to work.
Just after where I am saving the file, I quickly check the file size, set
the model''s attribute and re-save it. Kind of messy!
def upload
@asset = Asset.new(params[:asset])
if @asset.save
File.open("#{RAILS_ROOT}/public/files/#{@asset.filename_as_id}",
"wb")
do |f|
f.write(params[:asset][:newfile].read)
end
@asset.filesize = File.size("#{
RAILS_ROOT}/public/files/#{@asset.filename_as_id}")
@asset.save
redirect_to(:back)
else
render(:action => :get)
end
end
On 5/29/06, Jeff Ward <animikii@gmail.com> wrote:>
> I am checking the file size of an uploaded file and storing it in a
> filesize column. It will work just fine when the user uploads any files
> other than images (jpg,png,gif).
>
> Word, Excel, Powerpoint, .zip and more all work fine.
>
> This is my model asset.rb that handles the file upload.
>
> def newfile=(newfile_field)
> self.filename = base_part_of(newfile_field.original_filename)
> self.filetype = newfile_field.content_type.chomp
> self.filesize = File.size(newfile_field) ### It fails on this line for
> images...
> end
>
> The error I get is: "can''t convert StringIO into String"
>
> I''m sure there''s an easier method/property to get the
file size of the
> uploaded file. Like, reading the HTTP headers?
>
> How do they get the file size in the validator?
> validates_filesize_of :field, :in => 15.kilobytes..1.megabyte
>
> Thanks for any leads,
>
> Jeff
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/29805fcc/attachment.html
> def newfile=(newfile_field) > self.filename > base_part_of(newfile_field.original_filename) > self.filetype = newfile_field.content_type.chomp > self.filesize = File.size(newfile_field) ### It fails on this line for > images... > end > > The error I get is: "can''t convert StringIO into String"File.size tells you the size of the file you give it by name, eg File.size(''/etc/passwd/''). You have a StringIO object, which is just another type of IO object. You want to use newfile_field.size. -- Phillip Hutchings http://www.sitharus.com/
Of course! This works. Or course this works. :) Thanks, Jeff On 5/29/06, Phillip Hutchings <sitharus@sitharus.com> wrote:> > > def newfile=(newfile_field) > > self.filename > > base_part_of(newfile_field.original_filename) > > self.filetype = newfile_field.content_type.chomp > > self.filesize = File.size(newfile_field) ### It fails on this line > for > > images... > > end > > > > The error I get is: "can''t convert StringIO into String" > > > File.size tells you the size of the file you give it by name, eg > File.size(''/etc/passwd/''). You have a StringIO object, which is just > another type of IO object. You want to use newfile_field.size. > > -- > Phillip Hutchings > http://www.sitharus.com/ > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/c5c370e7/attachment.html