Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file form web-app

Tags:

grails

Inside a grails application, I need to upload a file under web-app/js, add a prefix, and put it in S3. I'm having trouble figuring out how to read the js file in a way that will work in development (/web-app/js) and production (/js). I'm doing this from inside a domain object.

like image 717
Steve Brewer Avatar asked Apr 04 '10 07:04

Steve Brewer


People also ask

How do you read a file in JavaScript?

To read a file, use FileReader , which enables you to read the content of a File object into memory. You can instruct FileReader to read a file as an array buffer, a data URL, or text.

How do I get the value of an input type file?

The value property returns the path or the name of the file selected with the <input type="file"> element. This property returns the name of the selected file with a fake path in IE, Google Chrome, and Opera, and the name of the selected file in Firefox and Safari.

Can JavaScript access local files?

Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.


1 Answers

In your controllers, you can call :

def jsFolder = grailsAttributes.getApplicationContext().getResource("js/").getFile()

and then proceed with jsFolder. To determine the base directory of a running Grails application, use

String dir = applicationContent.getResource("/").getFile()

Getting the js path from a service is a little bit tricky:

You need to implement the ApplicationContextAware interface like this :

class MyService implements ApplicationContextAware {
ApplicationContext applicationContext

However, calling this code from a domain class is not a good idea (see this thread for some explanations) and I am not even sure if it's possible except from getting paths from manual configurations

Hope it helps.

like image 122
fabien7474 Avatar answered Nov 07 '22 19:11

fabien7474