2

我创建了一个带有浏览按钮的视图,让用户选择一个文件,然后当他们单击上传按钮时,它应该在控制器中触发该功能。

我已经使用空函数设置了视图和控制器,并且一直在查看“ Grails CSV 插件”以帮助解析文件,但是从使用信息中我似乎无法掌握如何将这些 CSV 数据放入数组中?

任何人都可以帮我解决这个问题吗?提前致谢

这是我的控制器,它基本上是空的:

package com.smstool

import org.springframework.dao.DataIntegrityViolationException

class UploadController {

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

    def upload() {

        }

}

我的观点只包含这种形式:

<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>  

我刚刚在控制器中尝试了这个:

@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"
            }
    }



}

这不起作用,因为我收到以下错误,有什么想法吗?

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
4

2 回答 2

3

看法:

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

控制器,安装了 Grails CSV 插件:

def upload() {
    def file = request.getFile('file')
    def allLines = file.inputStream.toCsvReader().readAll()
    ...
}
于 2012-09-12T15:32:27.797 回答
0

要读取单行或第一行:

def upload() {
   def file = request.getFile('file')
   def aLine = file.inputStream.toCsvReader().readNext()
   ...
}
于 2014-07-04T14:53:28.737 回答