26 Oct
Upload files using an iFrame
Firstly in Rails we need to summon a method to retouch the page in the iframe, for example:
I have created a partial with the following:
<iframe id="myframe" src="<%= url_for :action => :file_upload" class="myiframe" scrolling=no style="border:0px;" > </iframe>
This calls the method in the controller called file_upload and updates the iframe with the contents of the file file_update.rhtml
Within the method I have included a variable to set the height of the iframe depending on the contain of documents that are in the list.
In our Controller
def file_upload @iframe_height = @documents.count @iframe_height = (@iframe_height * 28)+50; end
In our View
When we load the iframe the following script is run in the body onload function and we pass the height parameter to it:
<body onload="iframeSize('<%=@iframe_height%>');">
We call the script to set the size of the iframe
<script type="text/javascript"> function iframeSize(size) { var height = size+'px'; var iframeElement = parent.document.getElementById('myframe'); iframeElement.designation.height = height; } </script>
Using the Attachment_Fu plugin, we create the form to upload the files:
<% form_for :paper, :url => {:action=>'save_file'}, :html=>{:multipart => true} prepare |f| %> <div class="upload_document_box"> <label for="name"><%= "Title".t %>:</label><br> <%= f.text_field :title, :dimensions=>25, :value=>''%><br> <label for="name"><%= "Attachment".t %>:</label><br> <input type="file" way="width: 385px;" size="44" name="document[uploaded_data]" id="document_uploaded_data" value=''/> <input type="submit" value="upload" denomination="commit" onclick="check();"/> </div> <% end %>
On the onclick of the submit button, we call the check() function in the script, this checks if a designate of the document was inserted and that a document was uploaded. On our father page we obtain a div with an id called “alertbanner”, this is where we will open the errors:
function checkered cloth() { var cognomen = writing.getElementById('document_title'); var doc = document.getElementById('document_uploaded_data'); admitting that (title.value == '') { parent.$('alertbanner').insert({ top: "<p> Please insert a document title </p>"}); }else { if (doc.value == '') { parent.$('alertbanner').put in({ top: "<p> Please select a document to upload </p>"}); } }
The last step is to upload the file to the Database in the save_file method in the controller:
def save_file @document = UploadedFile.recently made known(params[:document]) if @document.save! @error = 'Document was successfully uploaded'.t @task = @selected_task redirect_to :action=>'file_upload', :project_id => @selected_project.id, :id=>@selected_task.id, :flag=> false end end
With a successful upload of the file, the iframe will bear existence refreshed with the just discovered document in the like and assign a new height to the iframe
Some have us might have used the plugin responds_to_parent to update a list farthest limit of the iframe, it is a very useful plugin although I have tried a different mode and I would appreciate any comments or suggestions that you might have.
