Discussion:
File Upload and g:formRemote
stuart clayman
2006-12-05 17:12:33 UTC
Permalink
I am trying to convert some file upload functionality from normal forms to
Ajax forms.

I have had success doing file upload using the approach outlined in the
online docs:

<g:form action="upload_handler" method="post"
enctype="multipart/form-data">
....
<input type="file" name="uploadFile" size="60" />
.....
</g:form>


In my controller I have:
def file = request.getFile('uploadFile')

which all works fine.

I replaced the g:form line for

<g:formRemote name="upload_form" url="[action:'upload_handler']"
onLoading="spinner('upload_results')"
update="[success:'message',failure:'error']" method="post"
enctype="multipart/form-data">

The behaviour I get is that data is uploaded, but I'm not conviced it gets
sent as a multipart form.
I think the data is being passed in slightly differently because I get the
following error messages:

[groovy] org.codehaus.groovy.runtime.InvokerInvocationException:
groovy.lang.MissingMethodException: No signature of method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}

.....

[groovy] Caused by: groovy.lang.MissingMethodException: No signature of
method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}


Does anyone have any ideas, or have they had success doing Ajax file upload


stuart
--
View this message in context: http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7703569
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Sven Haiges
2006-12-05 19:47:55 UTC
Permalink
Hi Stuart,

what ajax library are you using? Dojo?

I had some slightly different experience with (dojo) AJAX file uploads.
After changing to formRemote, the form submissions did no longer work for
me. I found out that the reason was that - if using Dojo - you must use
require dojo.io.IOForm (can't remember exactly the name, not in front of my
dev laptop). So I added this require and also replaced the g:formRemote tag
with custom javascript code at the same time, as I planned and now did do a
lot more very custom AJAX stuff and wanted to be more flexible.

Another thing I found out is that if you do file uploads and want to return
data in JSON format, that response has to be returned in an html textarea in
the response. Sounds strange, but that's how dojo does handle this if you
got file uploads. I think the problem is that the repsonse of the file
upload is of course received in the hidden IFrame.

In short: it get's tricky as soon as files have to be uploaded via AJAX
(dojo-style). If you like, I can post my sample code that works nicely
tomorrow.

Cheers\
Sven
Post by stuart clayman
I am trying to convert some file upload functionality from normal forms to
Ajax forms.
I have had success doing file upload using the approach outlined in the
<g:form action="upload_handler" method="post"
enctype="multipart/form-data">
....
<input type="file" name="uploadFile" size="60" />
.....
</g:form>
def file = request.getFile('uploadFile')
which all works fine.
I replaced the g:form line for
<g:formRemote name="upload_form" url="[action:'upload_handler']"
onLoading="spinner('upload_results')"
update="[success:'message',failure:'error']" method="post"
enctype="multipart/form-data">
The behaviour I get is that data is uploaded, but I'm not conviced it gets
sent as a multipart form.
I think the data is being passed in slightly differently because I get the
groovy.lang.MissingMethodException: No signature of method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}
.....
[groovy] Caused by: groovy.lang.MissingMethodException: No signature of
method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}
Does anyone have any ideas, or have they had success doing Ajax file upload
stuart
--
http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7703569
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Sven Haiges
***@googlemail.com

Skype: hansamann
Personal Homepage, Wiki & Blog: http://www.svenhaiges.de

Subscribe to the Grails Podcast:
http://hansamann.podspot.de/rss
stuart clayman
2006-12-05 20:48:26 UTC
Permalink
I am using Prototype and Scriptaculous.

I've had a closer look and I think the enctype="multipart/form-data" is
completely ignored.
I noticed that the returned form has a string argument, which is the
filename.
The contents of the file never reach the server - this is the real problem.

Does your custom AJAX send the actual file contents?

I would be interested in seeing it, to see if I can get a version to work
with Prototype.

stuart
Post by Sven Haiges
Hi Stuart,
what ajax library are you using? Dojo?
I had some slightly different experience with (dojo) AJAX file uploads.
After changing to formRemote, the form submissions did no longer work for
me. I found out that the reason was that - if using Dojo - you must use
require dojo.io.IOForm (can't remember exactly the name, not in front of my
dev laptop). So I added this require and also replaced the g:formRemote tag
with custom javascript code at the same time, as I planned and now did do a
lot more very custom AJAX stuff and wanted to be more flexible.
Another thing I found out is that if you do file uploads and want to return
data in JSON format, that response has to be returned in an html textarea in
the response. Sounds strange, but that's how dojo does handle this if you
got file uploads. I think the problem is that the repsonse of the file
upload is of course received in the hidden IFrame.
In short: it get's tricky as soon as files have to be uploaded via AJAX
(dojo-style). If you like, I can post my sample code that works nicely
tomorrow.
Cheers\
Sven
Post by stuart clayman
I am trying to convert some file upload functionality from normal forms to
Ajax forms.
I have had success doing file upload using the approach outlined in the
<g:form action="upload_handler" method="post"
enctype="multipart/form-data">
....
<input type="file" name="uploadFile" size="60" />
.....
</g:form>
def file = request.getFile('uploadFile')
which all works fine.
I replaced the g:form line for
<g:formRemote name="upload_form" url="[action:'upload_handler']"
onLoading="spinner('upload_results')"
update="[success:'message',failure:'error']" method="post"
enctype="multipart/form-data">
The behaviour I get is that data is uploaded, but I'm not conviced it gets
sent as a multipart form.
I think the data is being passed in slightly differently because I get the
groovy.lang.MissingMethodException: No signature of method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}
.....
[groovy] Caused by: groovy.lang.MissingMethodException: No signature of
method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile() is
applicable for argument types: (java.lang.String) values: {"uploadFile"}
Does anyone have any ideas, or have they had success doing Ajax file upload
stuart
--
http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7703569
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Sven Haiges
Skype: hansamann
Personal Homepage, Wiki & Blog: http://www.svenhaiges.de
http://hansamann.podspot.de/rss
--
View this message in context: http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7707546
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Sven Haiges
2006-12-06 08:56:43 UTC
Permalink
Hi Stuart,

please note that I am using dojo, here are some snippets out of my code that
work nicely:

VIEW

<form name="imageForm" id="imageForm"
action="/AjaxDemo/editor/addImage" method="POST"
enctype="multipart/form-data">
<input size="16" name="imageFile" type="file"
accept="image/*" />
<input type="submit" value="Add"/>

</form>

Tuned into AJAX form via DOJO FormBind: (please note, imageForm() is called
during the init(), e.g. after the DOM has been created in the browser)
function

imageForm() {
//ajax form
var form = new dojo.io.FormBind({
// reference your form
formNode: document.forms["imageForm"],
handle: function(type, data, evt)
{
if (type == "load")
{
//access json data
var fileName = data.imageElement.fileName;
...
}
else
{
quickInfo('Could not upload image. Please try again!',
'WARNING');
}
},
mimetype: "text/json"

});
}

Note the dojoConfig:

dojo.require("dojo.io.*");
dojo.require("dojo.io.IframeIO");

CONTROLLER action:

def addImage = {
log.info("addImage");

def f = request.getFile('imageFile')

if(f.empty) {
render "No file!"
}
else {
//do sth with the file

//json response wrapped in a textarea for dojo file uploads.
java.io.Writer writer = new java.io.StringWriter();
new grails.util.JSonBuilder(writer).json
{
imageElement(fileName:"/AjaxDemo/uploadImages/${
f.getOriginalFilename()}")
}
def jsonText = writer.toString()

render(text:"<html><body><textarea>${jsonText}</textarea></body></html>",contentType:"text/html",encoding:"UTF-8")


}
}


I hope that helps. I know there are a lot of tweaks in it, specially because
I wanted to have a json response because it is easier an cleaner to get the
data out of the object in the browser side javascript with that. This
required me to wrap the json string in a texarea and return a text/html
response... I think the current g:formRemote Tags do not support file
uploads, but please anyone please correct me if I should be wrong!

Cheers\
Sven
Post by stuart clayman
I am using Prototype and Scriptaculous.
I've had a closer look and I think the enctype="multipart/form-data" is
completely ignored.
I noticed that the returned form has a string argument, which is the
filename.
The contents of the file never reach the server - this is the real problem.
Does your custom AJAX send the actual file contents?
I would be interested in seeing it, to see if I can get a version to work
with Prototype.
stuart
Post by Sven Haiges
Hi Stuart,
what ajax library are you using? Dojo?
I had some slightly different experience with (dojo) AJAX file uploads.
After changing to formRemote, the form submissions did no longer work
for
Post by Sven Haiges
me. I found out that the reason was that - if using Dojo - you must use
require dojo.io.IOForm (can't remember exactly the name, not in front of my
dev laptop). So I added this require and also replaced the g:formRemote tag
with custom javascript code at the same time, as I planned and now did
do
Post by Sven Haiges
a
lot more very custom AJAX stuff and wanted to be more flexible.
Another thing I found out is that if you do file uploads and want to return
data in JSON format, that response has to be returned in an html
textarea
Post by Sven Haiges
in
the response. Sounds strange, but that's how dojo does handle this if
you
Post by Sven Haiges
got file uploads. I think the problem is that the repsonse of the file
upload is of course received in the hidden IFrame.
In short: it get's tricky as soon as files have to be uploaded via AJAX
(dojo-style). If you like, I can post my sample code that works nicely
tomorrow.
Cheers\
Sven
Post by stuart clayman
I am trying to convert some file upload functionality from normal forms to
Ajax forms.
I have had success doing file upload using the approach outlined in the
<g:form action="upload_handler" method="post"
enctype="multipart/form-data">
....
<input type="file" name="uploadFile" size="60" />
.....
</g:form>
def file = request.getFile('uploadFile')
which all works fine.
I replaced the g:form line for
<g:formRemote name="upload_form" url="[action:'upload_handler']"
onLoading="spinner('upload_results')"
update="[success:'message',failure:'error']" method="post"
enctype="multipart/form-data">
The behaviour I get is that data is uploaded, but I'm not conviced it gets
sent as a multipart form.
I think the data is being passed in slightly differently because I get the
groovy.lang.MissingMethodException: No signature of method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile
()
Post by Sven Haiges
Post by stuart clayman
is
{"uploadFile"}
Post by Sven Haiges
Post by stuart clayman
.....
[groovy] Caused by: groovy.lang.MissingMethodException: No signature
of
Post by Sven Haiges
Post by stuart clayman
method
org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest.getFile
()
Post by Sven Haiges
Post by stuart clayman
is
{"uploadFile"}
Post by Sven Haiges
Post by stuart clayman
Does anyone have any ideas, or have they had success doing Ajax file upload
stuart
--
http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7703569
Post by Sven Haiges
Post by stuart clayman
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Sven Haiges
Skype: hansamann
Personal Homepage, Wiki & Blog: http://www.svenhaiges.de
http://hansamann.podspot.de/rss
--
http://www.nabble.com/File-Upload-and-g%3AformRemote-tf2762970.html#a7707546
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Sven Haiges
***@googlemail.com

Skype: hansamann
Personal Homepage, Wiki & Blog: http://www.svenhaiges.de

Subscribe to the Grails Podcast:
http://hansamann.podspot.de/rss
Loading...