Search code examples
grails

how to upload file into server directory with grails?


how to upload file into server directory.. if my project at D:\myapp and i run with cmd d:\myapp grails run-app when i run this app and other Computer run it and upload file..it will save ini computer server in directory D:\myapp\upload ?

i try this ini gsp.

<g:form action="list" enctype="multipart/form-data" useToken="true">
    <span class="button">
        <input type="file" name="filecsv"/>
        <input type="button" class="upload" value="Upload"
               onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>
    </span>
</g:form>

def upload = {

    def f = request.getFile('filecsv')
    if (f.empty) {
        flash.message = 'file cannot be empty'
        render(view: 'list')
        return
    }

    f.transferTo(new File('C:\Users\meta\Documents\workspace-sts-2.5.2.RELEASE\wawet\wallet\uploads\file_name.csv'))
    response.sendError(200, 'Done')
}

this is the error :

2014-02-03 10:43:02,706 [http-8080-2] ERROR errors.GrailsExceptionResolver  - No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
groovy.lang.MissingMethodException: No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
        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)

Solution

  • The destination is only a file like in Java.

    def f = request.getFile('some_file')
    
    //validate file or do something crazy hehehe
    
    //now transfer file
    File fileDest = new File("Path to some destination and file name")
    f.transferTo(fileDest)
    

    OR if you want to store it at some path relative to user home:

    def homeDir = new File(System.getProperty("user.home")) //user home e.g /home/username for unix
    File fileDest = new File(homeDir,"path/to/some_folder")
    f.transferTo(fileDest)
    

    UPDATE As per why your getFile is not working, you are not submitting your form:

    <g:form action="list" enctype="multipart/form-data" useToken="true">
    
    <span class="button">                   
                        <input type="file" name="filecsv"/>
                        <input type="button" class="upload"
                                            value="Upload"
                                            onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>
    
                </span>
    
    </g:form>
    

    Should be:

    <g:form action="upload" enctype="multipart/form-data" useToken="true">
    
    <span class="button">                   
                        <input type="file" name="filecsv"/>
                        <input type="submit" class="upload" value="upload"/>
    
                </span>
    
    </g:form>
    

    if you need to use javascript you should submit the form instead of adding a link to another page.