2

I have created a view with browse button to let the user select a file and then when they click the Upload button it should fire that function in the controller.

I have setup the View and the Controller with the empty function and I have been looking at "Grails CSV Plugin" to aid in parsing the file but from the usage information I cant seem to grasp how I can get this CSV data into an array?

Can any1 please help me with this?? Thanks in advance

This is my Controller which is basically empty:

package com.smstool

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

    def index() {
        redirect(action: "upload", params: params)
    }

    def upload() {

        }

}

And my view simply contains this form:

<g:form action="upload" method="post" enctype="multipart/form-data">  
            <label for="file">File:</label>  
            <input type="file" name="file" id="file"/>  
            <input class="save" type="submit" value="Upload"/>  
        </g:form>  

I just tried this in the controller:

@Grab('com.xlson.groovycsv:groovycsv:1.0')
import static com.xlson.groovycsv.CsvParser.parseCsv

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

    def index() {
        redirect(action: "upload", params: params)
    }

    def upload() {


        }


    def batchUpload() {

            def csv = new File('file')

            //def csv = '''Name,Lastname
            //Mark,Andersson
            //Pete,Hansen'''

            def data = parseCsv(csv)

            for(line in data) {
                println "$line.Number"
            }
    }



}

This did not work as I get the following error, any ideas?

Error 500: Internal Server Error

URI
    /upload/batchUpload
Class
    groovy.lang.MissingMethodException
Message
    No signature of method: static com.xlson.groovycsv.CsvParser.parseCsv() is applicable for argument types: (java.io.File) values: [file] Possible solutions: parseCsv(java.io.Reader), parseCsv(java.lang.String), parseCsv(java.util.Map, java.io.Reader), parseCsv(java.util.Map, java.lang.String), parse(java.io.Reader), parse(java.lang.String)

Around line 28 of grails-app/controllers/com/tool/UploadController.groovy

25:         //Mark,Andersson26:         //Pete,Hansen'''27:         28:         def data = parseCsv(csv)29:         30:         for(line in data) {31:              println "$line.Number"

Around line 195 of PageFragmentCachingFilter.java

192:            if (CollectionUtils.isEmpty(cacheOperations)) {193:             log.debug("No cacheable annotation found for {}:{} {}",194:                     new Object[] { request.getMethod(), request.getRequestURI(), getContext() });195:               chain.doFilter(request, response);196:              return;197:         }198:

Around line 63 of AbstractFilter.java

60:     try {61:            // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping62:         if (filterNotDisabled(request)) {63:                doFilter(request, response, chain);64:          }65:            else {66:               chain.doFilter(req, res);

Trace

    Line | Method
->>   28 | batchUpload in UploadController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    195 | doFilter    in PageFragmentCachingFilter.java
|     63 | doFilter .  in AbstractFilter.java
|   1110 | runWorker   in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    679 | run         in java.lang.Thread
0

2 Answers 2

3

View:

<g:uploadForm action="upload">
    <input type="file" name="file">
    <g:submitButton name="upload" value="Upload"/>
</g:uploadForm>

Controller, with the Grails CSV plugin installed:

def upload() {
    def file = request.getFile('file')
    def allLines = file.inputStream.toCsvReader().readAll()
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

This did work and the information was stored. I have 3 rows of data with headers, and I only need to get the data for 1 row and store it into an array, how can i do this? Thanks
Can anyone please help on this?
0

To read a single line or first line:

def upload() {
   def file = request.getFile('file')
   def aLine = file.inputStream.toCsvReader().readNext()
   ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.