【发布时间】:2015-02-14 12:30:14
【问题描述】:
所以我有一个uploadForm,在我选择要上传的文件并提交之后,我想将文件文档的内容显示到textArea或Form,但我不知道如何获取该文件的参数和在该文件中呈现文本。
这是我的代码:文件displayForm.gsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Upload</title>
</head>
<body>
<div class="body">
</div>
<g:uploadForm action ="uploaded" >
<input type = "file" name = "cfile" value = "File"/>
<g:actionSubmit action="displayForm" value = "submit" />
</g:uploadForm>
<p>
<textField rows="50" cols="100" value = "${cfile}"
name="displayResult"/ >
</p>
<input type="button" value = "Display" name = "Display">
<br />
</body>
</html>
这是文件SampleController.groovy:
class SampleController{
def f
def displayForm() {
if(params.cfile){
if(params.cfile instanceof org.springframework.web.multipart.commons.CommonsMultipartFile){ //new FileOutputStream('D:/submitted_file.txt').leftShift( params.cfile.getInputStream() );
f=params.cfile.transferTo(new File('D:/submitted_file.uima'));
render file:params.cfile.toString() ,contentType: "text", encoding:"UTF-8"
}else{
log.error("wrong attachment type [${cfile.getClass()}]");
}
}
}
正如您在上面看到的,我不想读取文件'D:/submitted_file.uima' 并渲染它。
但是如果我调用渲染方法,则会出现文件为空的错误。
我在从文件.gsp 调用文件参数时遇到问题。我应该从uploadForm 和textArea(textField) 调用名称“cfile”或“value”或“id”或其他名称。
【问题讨论】:
标签: java grails web groovy gsp