Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble checking the size of an uploading image file using Ruby on Rails 3
    primarykey
    data
    text
    <p>I am using Ruby on Rails 3 and in my controller I'm just trying to check if the size of an uploading image is larger than a given value. </p> <p>I need this because I would like to prevent that the server, before saving the image file on the Hard disk, checks the uploading image so that the upload doesn't start if the image size is larger of a custom value: I would like prevent a sever <em>overloading</em>.</p> <p>I am trying to do that as follows:</p> <pre><code>if request.headers["CONTENT_LENGTH"] image_file_max_size ||= 16777216 @user.errors.add( :image, "is too big") if ( request.headers["CONTENT_LENGTH"].to_i &gt; image_file_max_size.to_i ) end # continue... (see UPDATES) </code></pre> <p>but it seems don't work. It seems to get the 'CONTENT_LENGTH' value only after that the upload is complete, not before.</p> <p>Why? What is wrong? Maybe the <code>request.headers["CONTENT_LENGTH"]</code> doesn't "intercept" the size of the uploading image?</p> <hr> <p><strong>UPDATE</strong> for <strong>@jamiew</strong> (<em>commenter</em>)</p> <p>My server setup is <em>Apache</em> + <em>Phusion Passenger</em>. I am also using <em>Paperclip</em> gem.</p> <p><strong>UPDATE</strong> for <strong>@Jeff Paquette</strong> (<em>commenter</em>)</p> <p>In my case I am running an <em>AJAX request</em> using this <a href="http://khamsouk.souvanlasy.com/articles/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent" rel="nofollow">technique</a>.</p> <pre><code>#continue if @account.errors.empty? if @account.update_attributes(params[:users_account]) flash.now[:notice] = "Good!" else flash.now[:notice] = "Bad!" end format.html { render :action =&gt; 'edit' } format.js { responds_to_parent do render :action =&gt; 'update.js.rjs' end } end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload