Skip to content

Commit

Permalink
Can upload a file from the computer or from a url.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed May 5, 2010
1 parent 3caf91b commit 56241b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
19 changes: 18 additions & 1 deletion app/models/inline_file_upload.rb
@@ -1,10 +1,27 @@

class InlineFileUpload < HashModel
attributes :file_id => nil, :object => nil, :field => nil
attributes :file_id => nil, :object => nil, :field => nil, :url => nil, :folder_id => nil, :from => 'computer'

validates_presence_of :file_id
validates_presence_of :object
validates_presence_of :file_id

integer_options :folder_id
domain_file_options :file_id

def handle_file_upload(renderer, params)
if ! params[:file][:url].blank?
begin
domain_file = DomainFile.create :filename => URI.parse(params[:file][:url]), :creator_id => renderer.myself.id
params[:file][:file_id] = domain_file.id if domain_file.id
self.file_id = domain_file.id
rescue Exception => e
Rails.logger.error e
end
else
renderer.handle_file_upload(params[:file], 'file_id', {:folder => self.folder_id})
self.file_id = params[:file][:file_id]
end
end

end
16 changes: 15 additions & 1 deletion app/views/inline_file_upload/_form.html.erb
@@ -1,6 +1,7 @@
<%
form_options[:html] ||= {}
form_options[:html][:multipart] = true
form_options[:html][:onsubmit] = '$("upload_form").hide(); $("uploading").show();'
-%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Expand All @@ -17,10 +18,23 @@ form_options[:html][:multipart] = true

<div id="upload_form">
<% cms_unstyled_form_for :file, file, form_options do |f| -%>

<div class="buttons">
Upload a file
<label><%= f.radio_button :from, 'computer', :onclick => "$('from_url').hide(); $('from_file').show();" %> from computer</label>
<label><%= f.radio_button :from, 'web', :onclick => "$('from_file').hide(); $('from_url').show();" %> from web</label>
</div>

<%= hidden_field_tag :upload, 1 %>
<%= f.hidden_field :object %>
<%= f.hidden_field :field %>
<%= f.file_field :file_id, :onchange => '$("upload_form").hide(); $("uploading").show(); this.form.submit();' %>
<div id="from_url" <%= 'style="display:none;"' unless file.from == 'web' %>>
<%= f.text_field :url, :value => 'http://' %>
<%= f.submit 'Fetch'.t %>
</div>
<div id="from_file" <%= 'style="display:none;"' unless file.from == 'computer' %>>
<%= f.file_field :file_id, :onchange => 'this.form.submit();' %>
</div>
<% end -%>
</div>

Expand Down
2 changes: 1 addition & 1 deletion lib/webiva_form_elements.rb
Expand Up @@ -1022,7 +1022,7 @@ def add_page_selector(field,options={ })

def inline_file_upload(field, options={})
options[:width] ||= '100%'
options[:height] ||= 35
options[:height] ||= 50
options[:frameborder] ||= 0
options[:marginwidth] ||= 0
options[:marginheight] ||= 0
Expand Down

0 comments on commit 56241b9

Please sign in to comment.