0

I've read the answers to the question asked here : How to upload file to remote FTP Server in grails

everything compiles and runs without errors. Now how do I reference the upload service from a Grails form? Sorry for the basic question.

So far I'm playing around with

          <g:form action="do_something" enctype="multipart/form-data" useToken="true">

            <span class="button">                   
            <input type="file" name="thefile"/>
            <input type="submit" class="upload" value="upload"/>

            </span>

          </g:form>

but I just need a few pointers as to how to link this with the service, presumably via the controller.

Community
  • 1
  • 1
Simon
  • 97
  • 3
  • 13

1 Answers1

0

Work with the request. Get the file name and create a new file. my own code:

def f = request.getFile('myfile'); 
def webrootDir = servletContext.getRealPath("/"); //app directory 
File fileDest = new File(webrootDir,"xmls/"); 
def xmlFile = new File(fileDest, "settings.xml"); 
f.transferTo(xmlFile);


Just look at this post for more info.

Community
  • 1
  • 1
Mike B
  • 2,756
  • 2
  • 16
  • 28
  • Thanks. This helps, and I can now upload files, but I'm having problems with filenames. If I follow the post you signposted me to, the filename of the file I'm uploading ends up as 'some_folder' and it is in the images directory. I create the folder images/some_folder but it just overwrites the folder some_folder with the actual file. So I end up with a file in the images folder, and the file is called some_folder. What I want is to put the file into some_folder with the filename of the file I'm uploading. – Simon Apr 14 '16 at 11:01
  • Are you sure you did everything correctly? That post shows that you create a directory and copy the file **into** that directory. – Mike B Apr 14 '16 at 11:19
  • @Simon - that is the best way of learning. mistakes you make help you understand things. Others telling you everything - doesn't help too much. – Mike B Apr 14 '16 at 11:20
  • I'm pretty sure I did. By the way, I'm using grails 2.1.1 and Netbeans 7.2.1 – Simon Apr 14 '16 at 11:22
  • I agree, but the Grails docs dont help. If I had easy to access docs I'd be fine. – Simon Apr 14 '16 at 11:23
  • Anyway, here is what I'm doing now :- def f = request.getFile("filewav") String thename = f.fileItem.name String destfile = "images/wavs/" + thename def webrootDir = servletContext.getRealPath("/") File fileDest = new File(webrootDir,destfile) f.transferTo(fileDest) – Simon Apr 14 '16 at 11:26
  • and the error I'm getting is Grails_Apps\c3i\web-app\images\wavs\30649-PG2.wav (The system cannot find the path specified) – Simon Apr 14 '16 at 11:27
  • I am confused now. It should work. my own code: `def f = request.getFile('myfile'); def webrootDir = servletContext.getRealPath("/"); //app directory File fileDest = new File(webrootDir,"xmls/"); def xmlFile = new File(fileDest, "settings.xml"); f.transferTo(xmlFile);` – Mike B Apr 14 '16 at 11:38
  • Got it. You are creating an additional object, of type File, in the line def xmlFile = new File (fileDest, "settings.xml"). I was missing that step! Many thanks for your help. I think I'm there now. – Simon Apr 14 '16 at 11:43
  • Great. When You finish, i'd appreciate if You accepted my answer. – Mike B Apr 14 '16 at 11:44