Posted on 2008-03-29 09:44
kooyee 阅读(923)
评论(1) 编辑 收藏 所属分类:
Ajax学习手记
首先下载dojo和安装dojo plugin (grails 1.0以上)
<g:javascript library="dojo" />
<g:javascript>
dojo.require("dojo.io.IframeIO");
</g:javascript>
<g:form url="[action:'submitToRemoteCall']" id="form2"
enctype="multipart/form-data">
File: <input name="someFile" type="file"></input>
<g:submitToRemote
value="Submit Upload"
name="form2"
action="submitToRemoteUpload"
update="[success:'message',failure:'error']" />
</g:form>
注意 form中写入url属性, 而且form不要有hide类型的input,否则会调用XMLHttpRequest,而不用IframeIO
在controller中写入
def submitToRemoteUpload =
{
def f = request.getFile('someFile')
if(f.empty) {
render "No file!"
} else {
def fileName = f.getOriginalFilename()
render(text:"<html><body><textarea>You called ${actionName} in
${controllerName} with file ${fileName}</textarea></body></html>",
contentType:"text/html", encoding:"UTF-8")
}
}
IframeIO的默认mimetype为text/plain,所以使用render (text:...)而且套嵌在<textarea>中
如果要返回render view。 可以修改dojo的dojo.io.bind(),bind参数加上mimetype:"text/html"。form中的<g:submitToRemote>改为
<input onclick="dojo.io.bind({url:'/test/index/submitToRemoteUpload',
mimetype:'text/html',
load:function(type,data,evt){dojo.byId('message')innerHTML = data.body.innerHTML;},
error:function(type,error) { dojo.html.textContent( dojo.byId('error'),error.message);},
formNode:dojo.byId('form2')});return false"
type="submit" name="form2" value="Submit Upload">