<input type="file" name="filecsv"/>
<input type="button" class="upload" value="Upload
onclick='location.href ="${createLink(url: [action: 'upload'])}"'/>
我在groovy中迷惑..我尝试了这样的代码,但没有成功.
def upload = {
println params.filecsv
new File('filecsv').splitEachLine(',') {fields ->
def city = new City(
city: fields[0].trim(),
description: fields[1].trim()
)
if (city.hasErrors() || city.save(flush: true) == null) {
log.error("Could not import domainObject ${city.errors}")
}
log.debug("Importing domainObject ${city.toString()}")
}
Parse CSV and export into Mysql database in Grails
解决方法:
您需要从MultipartFile获得MultipartFile的InputStream
<g:uploadForm action="upload">
<input type="file" name="filecsv" />
<input type="submit" />
</g:uploadForm>
然后;
def upload = {
request.getFile( 'filecsv' )
.inputStream
.splitEachLine(',') { fields ->
def city = new City( city: fields[0].trim(),
description: fields[1].trim() )
if (city.hasErrors() || city.save(flush: true) == null) {
log.error("Could not import domainObject ${city.errors}")
}
log.debug("Importing domainObject ${city.toString()}")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。