Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

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:myappupload ?

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:UsersmetaDocumentsworkspace-sts-2.5.2.RELEASEwawetwalletuploadsfile_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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...