【发布时间】:2017-07-24 21:52:17
【问题描述】:
我有一个页面,它允许随着时间的推移逐步构建 PDF 文件(将信息保存到数据库)。我还添加了保存图像的功能。我目前有一个<g:uploadForm> 将文件发送回控制器中的upload 方法(然后调用服务)。然而,在上传完成后,Grails 会尝试渲染一个与upload 方法相关联的视图,结果是一个空白页面。我希望拥有将文件发送到后端 Grails 的功能,而无需重新加载视图(因为它会删除任何已输入但未保存的数据)。我还有一个使用 JavaScript 向 Grails 服务器发出 AJAX 请求的工作版本,但我更喜欢 Grails 解决方案。
这是 GSP 的相关部分:
<g:uploadForm action="upload" useToken="true" id="5">
<div class="chooseFileButtonContainer">
<input type="file" name="imgfile" class="chooseFileButton" />
</div>
<div class="uploadFileButtonContainer">
<input type="button" value="Upload File" />
</div>
</g:uploadForm>
这是控制器:
def uploadToSP() {
if (params.imgfile) {
if (params.imgfile instanceof org.springframework.web.multipart.commons.CommonsMultipartFile) {
def (sectionID, reportID) = params.id.tokenize(':')
try {
Service.upload(params.imgfile, sectionID, reportID)
} catch (Exception e) {
System.err << "Upload Failed : " + e.getMessage() +"\n"
}
} else {
System.err << "Invalid file format\nExpected org.springframework.web.multipart.commons.CommonsMultipartFile\nGot ${params.imgfile.getClass()}\n"
}
} else {
System.err << "No file supplied\n"
}
response.sendError(200, "Done")
}
除了单击Upload 按钮后,它会转到像mysite.com/myapplication/controller/upload/id:id 这样的URL,这只是一个空白页面(/id:id 部分是故意的),一切都运行良好。
我可以使用 JavaScript 做到这一点,但我对仅 Groovy/Grails 的解决方案感兴趣。 我的问题是如何在不加载新视图的情况下使用控制器方法。
【问题讨论】:
-
@MikeW 这一切似乎只是呈现
done或['result': 'success']而不是空白页 -
I want to have the functionality of sending the file to the back-end Grails without so much as reloading the view如果我理解正确,那么您的解决方案将是 websockets,它将在 groovy grails 中执行后端操作,但是当它更新视图时,它只能通过前端订阅者接收的 javascript 事件来实现websocket 连接。
标签: grails groovy gsp grails-controller