Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Uploading and Downloading Files.

Similar presentations


Presentation on theme: "Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Uploading and Downloading Files."— Presentation transcript:

1 Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Uploading and Downloading Files

2 This is an example application that implements a simple document management system. The goal is to show how to upload or download documents in a Grails application. Task Description 2

3 The user can view list of documents: Task Description 3

4 The user can view list of documents: Clicking an item will download the document file. Task Description 4

5 There is a form to upload and add a document to the repository: Clicking “Upload” will save the document file into predefined folder. Task Description 5

6 As this is just a sample application, there is only one domain in the application to represent a document: package com.dms class Document { String filename String fullPath Date uploadDate = new Date() static constraints = { filename(blank:false,nullable:false) fullPath(blank:false,nullable:false) } The filename is to preserve the original filename uploaded by the user. The fullPath is where the document is saved after upload. The uploadDate is just for keeping track of the date and time the document is uploaded. Solution. Step 1: Domain 6

7 1.Run grails generate-controller com.dms.Document 2.Open DocumentController.groovy. Do not delete existing actions. 3. Update your class DocumentController adding new action list() as shown below. 4.Modify index() action as shown below. class DocumentController { def list() { params.max = 10 [documentInstanceList: Document.list(params), documentInstanceTotal: Document.count()] } def index(Integer max) { redirect (action: "list", params: params) } } Solution. Step 2: Controller 7

8 10 records per page will be shown : list.gsp Note the entry with: ${documentInstance.filen ame} This is the link to download the file given the document id. Solution. Step 3: View 8

9 We can configure where to upload the documents by introducing the configuration variable uploadFolder in Config.groovy. We can have different values depending on the environment. 1. Create new empty folder C:\dms\upload 2. Append the following at the bottom of Config.groovy: environments { development { uploadFolder = "c:/dms/upload/" } test { uploadFolder = "c:/dms/upload/" } production { uploadFolder = "c:/dms/upload/" } Solution. Step 4: Configuring upload location 9

10 Document upload is simple. For the form, we can use the tag uploadForm. create.gsp Solution. Step 4: Document Upload 10

11 Update your DocumentController.groovy by adding the upload() action, do not delete existing code. The code will be explained in the next slide: def upload() { def file = request.getFile('file') if(file.empty) { flash.message = "File cannot be empty" } else { def documentInstance = new Document() documentInstance.filename = file.originalFilename documentInstance.fullPath = grailsApplication.config.uploadFolder + documentInstance.filename file.transferTo(new File(documentInstance.fullPath)) documentInstance.save() } redirect (action:'list') } Solution. Step 5: Adding upload() action to DocumentController 11

12 We can access the uploadFolder we set in Config.groovy using the code grailsApplication.config.uploadFolder. The file uploaded can be accessed through request.getFile('file'). The following properties/methods were used: file.empty - check if there is really an uploaded file file.originalFilename - the filename of the document uploaded file.transferTo - transfer the file to a new path. Solution. Step 5: Adding upload() action to DocumentController (explanation) 12

13 For downloading, we just need to retrieve the file from the file system and send it to the response. Update your Document Controller with download() action (do not delete existing code): def download(long id) { Document documentInstance = Document.get(id) if ( documentInstance == null) { flash.message = "Document not found." redirect (action:'list') } else { response.setContentType("APPLICATION/OCTET-STREAM") response.setHeader("Content-Disposition", "Attachment;Filename=\"${documentInstance.filename}\"") def file = new File(documentInstance.fullPath) def fileInputStream = new FileInputStream(file) def outputStream = response.getOutputStream() byte[] buffer = new byte[4096]; int len; while ((len = fileInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } outputStream.flush() outputStream.close() fileInputStream.close() } Solution. Step 6: Document Download 13

14 The calls to response.setContentType("APPLICATION/OCTET- STREAM") and response.setHeader("Content- Disposition", "Attachment;Filename=\"${documentInstance.filena me}\"") are necessary to force the browser to download. Otherwise the browser will try to render the document. The rest of the code is just to read the file and send it to the response object. Solution. Step 6: Document Download (explaining the code) 14

15 The above code is just to serve as a working example on how to code uploading and downloading of files. To help start projects that needs such feature. Remarks 15


Download ppt "Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Uploading and Downloading Files."

Similar presentations


Ads by Google