4

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)
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156

3 Answers3

17

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.

Emmanuel John
  • 2,296
  • 1
  • 23
  • 30
  • 2
    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) –  Feb 03 '14 at 04:00
  • @unekwu it's very bad idea to put uploaded files into webroot, it will be completely removed on app restart. and anyway, the question was about `getFile` – Igor Artamonov Feb 03 '14 at 04:27
  • @unekwu it depends on container actually, but Tomcat, for example, removes app directory on each WAR unpack (of course, if you're using WARs) – Igor Artamonov Feb 03 '14 at 05:20
  • @unekwu 2014-02-03 14:11:11,034 [http-8080-1] ERROR errors.GrailsExceptionResolver - No such property: params for class: com.teravin.wallet.LoanAccountController org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: params for class: com.teravin.wallet.LoanAccountController –  Feb 03 '14 at 07:12
  • why can be like that?images/some_folder" this folder must i create first or it will automatically create it? sory i'm a beginner and thanks you and for @IgorArtamonov too.. –  Feb 03 '14 at 07:14
  • @akiong Like Igor said its a bad idea to save in the webroot. I would suggest using `System.getProperty("user.home")` to get the home directory, then you can retrieve the path to some folder from there. I don't know if this will work on cloud servers though. And yes you have to create the folder first. – Emmanuel John Feb 03 '14 at 09:23
2

The location of your Grails app doesn't matter. You have to specify the full destination path in your controller. Here is an example

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

    f.transferTo(new File('D:\myapp\upload\file_name.txt')) 
    response.sendError(200, 'Done') 
}
Rami Enbashi
  • 3,526
  • 1
  • 19
  • 21
  • 'D:\myapp\upload\file_name.txt' this is an absolute path?how if myapp is in E:\myapp? –  Feb 03 '14 at 03:37
0

final String IMAGE_DIR = "${servletContext.getRealPath('/images')}/";

    def employeeId = "dablu_photo";

    def employeePicture = request.getFile("cv_");

    String photoUrl  ="";
    if (employeePicture && !employeePicture.empty) {
        if (new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png")?.exists()){
            FileUtils.deleteQuietly(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"));
        }
        employeePicture.transferTo(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"))

    }
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. –  Jun 24 '20 at 10:01