问题
I am using Ruby on Rails 3 and in a my view file I have this code:
<%= form_for (@account, ...) do |f| %>
<%= f.file_field :file %>
...
<%= f.submit "Upload" %>
<% end %>
In order to avoid an overloading of the server, I would check the size of the uploading file before that the server receives it. This is because, pressing the submit button of the form, the server first will entirely receive the file and after will check the file.
I know that a HTTP request has header fields, so I would like to check those for the uploading file, in particular, in my case, the Content-Length value.
How to do that?
回答1:
Did you try request.headers["content-length"]
?
回答2:
Doesn't nginx set a max file size too? Using that as your 'base' you can assure that the server will stop a big ass file upload. Maybe set it to the same max size if you are worried about this.
来源:https://stackoverflow.com/questions/4837014/how-to-check-the-field-content-length-of-an-http-request-using-ruby-on-rails