# Image upload problem with ajax

**URL:** <https://www.ruby-forum.com/t/image-upload-problem-with-ajax/86961>\
**Category:** Rails\
**Created:** [February 27, 2007, 6:01am UTC](https://www.ruby-forum.com/t/image-upload-problem-with-ajax/86961 "2007-02-27T06:01:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 27, 2007, 6:01am UTC](https://www.ruby-forum.com/t/image-upload-problem-with-ajax/86961/1 "2007-02-27T06:01:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 27, 2007, 9:35am UTC](https://www.ruby-forum.com/t/image-upload-problem-with-ajax/86961/2 "2007-02-27T09:35:14Z")

</div>

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/](http://svn.techno-weenie.net/projects/)  
mongrel\_upload\_progress/)  
Â• SWFUpload ([http://swfupload.mammon.se/](http://swfupload.mammon.se/)): uses Flash for the upload,  
but works really great

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 27, 2007, 9:51am UTC](https://www.ruby-forum.com/t/image-upload-problem-with-ajax/86961/3 "2007-02-27T09:51:17Z")

</div>

> 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 😉

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](http://www.ruby-forum.com/topic/67408)) , 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](http://www.gastosgem.com) !!Es gratis!!
