📜 ⬆️ ⬇️

Uploading files via Ajax

Quite an interesting method of downloading a file, although I think many people know it.


To begin with, we will create a form and a hidden iframe in which the form itself will be sent (this is indicated by the target attribute).
<br>
<br>
/><br>
/><br>
<br>

<br>

When the Submit button is clicked, the form will be sent to the hidden iframe, and the controller's “upload_action” method will be called. Now you need to get data in the context of the entire page (the parent window for the frame). It uses a very interesting solution. We describe the controller for receiving and processing data from this form:
<br>
class UploadController < ActionController::Base<br>
def upload_action<br>
responds_to_parent do<br>

File.open("public/files/upl_file",'wb') do |file| <br>
file.write params[:upl_data].read <br>
end <br>
render :update do |page|<br>
page.replace_html 'upload_frm', :partial => 'upload'<br>

end<br>
end<br>
end <br>
end<br>

The call to the iframe is implemented by the respond_to_parent plugin. To generate such a form with an iframe, you can use the remote_upload plugin.

The decision in my opinion is very nice.

')

Source: https://habr.com/ru/post/11358/


All Articles