java - How to create a new directory in grails application to store jasper report files those can be downloaded by user -


i want create new directory store pdf reports generated jasper reports , should accessible users download

for example: exporting file called "pdfreport20110804788.pdf".when put file in directory .i set things in controller method , need file created returned groovy method new directory created , exported pdf file file should returned controller calling method further operations

response.setcontenttype("application/octet-stream") response.setheader("content-disposition", "attachment;filename=${file.getname()}") response.outputstream << file.newinputstream() 

so ,i have been trying these thing creating file using createtempfile method , returning file controller file getting downloaded c:\ ... location without asking download or open .can 1 please give me solution this

there 2 things need aware of:

1.) files generated on server's disk cannot directly accessed user on web site. have deliver files through browser via output stream.

2.) you cannot freely access disk of user on web site. so, if web site creates file on "c:\website\file.pdf" go server's "c:" drive , never end users "c:" drive. remember "c:\" drive windows thing unix or mobile users not able use site if because don't have "c:" drive.

so, here if want store files , allow user download them or later (not tested , better shows concept)...

create class represents directory of reports

class reportdirectory{     static final string path = "./path/to/reports/"; //<-- generic path on server!     static{        //static initializer make sure directory gets created.  better ways work!         file pathasfile = new file(path).mkdirs()         if (pathasfile.exists()){           println("created report directory @ ${pathasfile.absolutepath}");        }else{           println("failed create report directory @ ${pathasfile.absolutepath}");        }     }     public static file[] listfiles(){        return new file(path).listfiles(); //<-- maybe use filters pull pdfs?    }     public static void addfile(file file){        filesutil.copyfiletodirectory(file, new file(path)); //<-- using apache-commons-io    }     public static void deleteall(){        listfiles().each(){ filetodelete ->            filetodelete.delete();        }    }     public static file findfile(string name){        listfiles().each(){ filetocheck ->             if (filetocheck.name.equals(name)){                return filetocheck            }        }        return null    }  } 

then in controller things this....

class reportcontroller{     def runreport = {        file report = createreport() //<-- method create report.        reportdirectory.addfile(report);         redirect(action:"downloadfle" params:[filename:report.name])     }          def showallfiles = {        [files:reportdirectory.listfiles()]    }     def downloadfile = {       def filename = params.filename;        def filetodownload = reportdirectory.findfile(filename);        if (filetodownload){        response.setcontenttype("application/octet-stream")       response.setheader("content-disposition", "attachment;filename=${filetodownload .getname()}")       response.outputstream << filetodownload.newinputstream()  //<-- ask user download      }else{          //handle when file not found      }     }     def deleteallfiles ={        reportdirectory.deleteallfiles()         [files:reportdirectory.listfiles()] //<-- return remaining files, if any.    }   } 

a few comments solution...

-this doesn't address mime types browser won't able figure out kind of binary data coming on wire.

is helpful?


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -