How to get File in grails? I tried this code but it's not success..
def upload = {
withForm{
def f = request.getFile('filecsv')
def orifilename = f.getOriginalFilename()
def homeDir = new File(System.getProperty("user.home"))
def homeurl = "Documents/Uploads/"
File fileDest = new File(homeDir,homeurl+orifilename)
f.transferTo(fileDest)
def card = Card
def cif = Cif
request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
def loanaccount = new LoanAccount(
totalLoanAmount: fields[0].trim(),
outstandingAmount: fields[1].trim(),
installmentAmount: fields[2].trim(),
collectionDate: fields[3].trim(),
dueDate: fields[4].trim(),
interestRate: fields[5].trim(),
card:card.findAllByCardNo(field[6]).trim(),
cif :cif.findAllByFirstName(field[7]).trim()
)
if (loanaccount.hasErrors() || loanaccount.save(flush: true) == null)
{
log.error("Could not import domainObject ${loanaccount.errors}")
}
}
redirect(action:"list")
}
}
Why it error?
But it's error to get the file...
2014-02-04 15:38:50,648 [http-8080-11] ERROR errors.GrailsExceptionResolver - Could not find matching constructor for: java.io.File(java.io.File)
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(java.io.File)
at com.teravin.wallet.LoanAccountController$_closure12_closure16.doCall(com.teravin.wallet.LoanAccountController:322)
at com.teravin.wallet.LoanAccountController$_closure12_closure16.doCall(com.teravin.wallet.LoanAccountController)
at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController:308)
at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController)
at java.lang.Thread.run(Thread.java:744)
I am using Grails 2.1.1
. I tried to find some documentation in google but still not able to solve my problem.
The problem is here
request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
You don't need request.getFile
here as you've already handled the file upload and transferred it to a temporary file, just use
fileDest.splitEachLine(',', 'UTF-8') { fields ->
Note that you should probably use an explicit encoding for splitEachLine
rather than relying on the platform default being correct. You could maybe examine the Content-Type sent by the browser to see if that specifies a charset, then fall back on a default of UTF-8 (or whatever) if it doesn't.
You also have a typo further down where you refer to field[6]
and field[7]
when the variable in question is actually called fields
with an s.