jeudi 6 mars 2014

How to read a file upload using remotipart Ruby on Rails


Vote count:

0




Hey guys i am trying to upload a file using the remotipart gem, so i got to the point where the files is submitted and i get the following in the console:



Parameters: {"utf8"=>"✓", "authenticity_token"=>"b3+vGGBgjaxZ5W7UhJIZfqK9vz7MsAi1ipeDrDV1vgo=", "commit"=>"Upload", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01", "uploaded_file"=>{"uploaded_file"=>#<ActionDispatch::Http::UploadedFile:0xb5f72ac8 @tempfile=#<Tempfile:/tmp/RackMultipart20140306-2294-1ifwfm5>, @original_filename="INSTRUCTION.txt", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"uploaded_file[uploaded_file]\"; filename=\"INSTRUCTION.txt\"\r\nContent-Type: text/plain\r\n">}}


So i have the file, but i do not know how should i read it.. I do not want to store the file, i just want to read it. Also, out from the parameters, i can get the parameter "uploaded_file":



params[:uploaded_file]


But in this case, how can i get one of its contents? I mean if i want to get the value of @headers inside the uploaded_file parameter, how can i do that? Also for some reason the javascript file attached to the current action is not working, here is my view:



= form_for(:uploaded_file, :remote => true, :url => {:action => 'parseFile'}, :html => {:multipart=>true}, :authenticity_token => true) do |f|
div class="browse"
span
| Choose file..

= f.file_field :uploaded_file

div id="file-status"
| You have not selected any files yet.

= f.submit :value => "Upload", :class => 'hidden'


And here is my controller:



def parseFile
:format == 'js'

if params[:remotipart_submitted]
respond_to do |format|
format.js
end
end

render :nothing => true
end


The parseFile.js.erb has the following contents:



alert(1);


And it never gets invoked.. Any help will be superb!


Thanks in advance! {:



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire