Image upload problem with ajax

hi friends,

when i upload image with submit_to_remote tag it shows me nil value when
i receive value of file_field in controller.

controller code

if params[:realestate_detail][:image].original_filename != “”
file = params[:realestate_detail][:image].original_filename
Dir.mkdir("#{RAILS_ROOT}/public/images/electronics_images/")
unless File.exist?("#{RAILS_ROOT}/public/images/electronics_images")
f=File.new("#{RAILS_ROOT}/public/images/electronics_images/" +
file, “w”)
data = params[:realestate_detail][:image].read
f.write(data)
@ele_gadg.image="/electronics_images/" + file
render :text=>data
else
render :text=>“No image”
end

.rhtml code

<%=

start_form_tag({:action=>“t”,:controller=>“real_estate”},{:multipart=>true,:name=>“frmV”})%>
<%=file_field ‘realestate_detail’,‘image’%>
<%= submit_to_remote(“submit”,“submit”,:url=>{:action =>
‘t’,:controller=>‘real_estate’})%>
<%= end_form_tag%>

please help me somebody.

On 27 Feb 2007, at 06:01, Trupti B. wrote:

when i upload image with submit_to_remote tag it shows me nil value
when
i receive value of file_field in controller.

You can’t upload files using AJAX, you’ll have to use one of the many
workarounds that are available (use google to search for “ajax
upload”), but here’s a few options you might consider:

• mongrel_upload_progress (http://svn.techno-weenie.net/projects/
mongrel_upload_progress/)
• SWFUpload (http://swfupload.mammon.se/): uses Flash for the upload,
but works really great

Best regards

Peter De Berdt

when i upload image with submit_to_remote tag it shows me nil value when i receive value of file_field in controller.

Because of javascript security, it’s not possible to access local files
directly, so in pure javascript/ajax you will not be able to upload a
file. Fortunately, there is a workaround for this. Remember the times
before ajax when everything was done behind the scenes by using a hidden
Iframe? well… time for going back in time :wink:

Basically for submitting a file via javascript and an IFrame what you do
is setting a hidden IFrame, and submit your form as usual (not remotely)
but targetting the IFrame. This way, the form is sent as always to the
server, but the user doesn’t lose the navigation, since the response of
the server is sent to the IFrame, which is hidden. By using javascript
from the response you can then manipulate your page and send a message
to the user.

There are plenty of solutions for this problem both out of rails and for
rails. One that works is using “responds_to_parent” plugin
([ANN] IFRAME based RJS - responds_to_parent - Rails - Ruby-Forum) , which makes the trick of
sending javascript back to the iframe.

regards,

javier ramirez


Estamos de estreno… si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!