简体   繁体   中英

Groovy: Save gform input type file to the assets pipeline (or similar)

(Sorry if this is simple; this is my first post)

Is the groovy/grails asset pipeline modifiable at runtime?

Problem: I am creating an application where users create the objects. The objects are stored as text files so that only the necessary objects are built at runtime. Currently, the text file includes a string which represents the filename of the image. The plan was to have these images stored in assets/images/ as this works best for later displaying the object. However, now I am running into issues with saving files to assets/images/ at run time, and I can't even figure out if this is possible. *Displaying images already works in the way I require if I drag and drop the images into the desired folder, however I need a way for the controller to put the image there instead. The relevant section of controller code:

    def folder = new File("languageDevelopment/grails-app/assets/images/")
    //println folder
    def f = request.getFile('keyImage');
        if (f.empty) 
        {
            flash.message = 'file cannot be empty'
            render(view: 'create')
            return
        }

        f.transferTo(folder)

The error I'm receiving is a fileNotFoundException "/var/folders/9c/0brqct9j6pj4j85wnc5zljvc0000gn/T/languageDevelopment/grails-app/assets/images (No such file or directory)"

on f.transferTo(folder)

What is the section it is adding to the beginning of my "folder" object?

Thanks in advance. If you need more information or have a suggestion to a different route please let me know!

new File("languageDevelopment/grails-app/assets/images/")

This folder is present only in your sources

After deployment it will looks like "/PATH-TO-TOMCAT/webapps/ROOT/assets/" if you use tomcat.

Also asset/images, asset/font etc. will be merged to assets folder.

If you'd like to store temporary files you can create some directory under src/resources folder.

For example "src/resources/images"

And you can get access to this folder from classloader:

this.class.classLoader.getResource('images/someImage.png').path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM