簡體   English   中英

如何在 grails 應用程序中創建一個新目錄來存儲用戶可以下載的碧玉報告文件

[英]How to create a new directory in grails application to store jasper report files those can be downloaded by user

我想創建一個新目錄來存儲由 jasper 報告生成的所有 pdf 報告,用戶也應該可以下載它們

例如:我正在導出一個名為“pdfreport20110804788.pdf”的文件。當我把這個文件放在那個目錄中時。我在 controller 方法中設置了某些東西,我需要創建的文件從 groovy 方法返回,其中新目錄是創建並將其導出為 pdf 文件,以便將文件返回到 controller 調用方法以進行進一步操作

response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
response.outputStream << file.newInputStream()

所以,我一直在嘗試通過使用 createTempFile 方法創建文件並將文件返回到 controller 但文件被下載到 c:\... 位置而不要求下載或打開。任何人都可以給我解決方案

您需要注意兩點:

1.)您的 web 站點上的用戶無法直接訪問服務器磁盤上生成的文件 您必須通過瀏覽器通過 output stream 傳遞文件。

2.)您不能在 web 站點上自由訪問用戶的磁盤 因此,如果您的 web 站點在“c:\website\file.pdf”上創建一個文件,它將始終 go 到服務器的“c:”驅動器,而不是最終用戶“c:”驅動器。 請記住,“c:\”驅動器主要是 Windows 的東西,因此 Unix 或移動用戶將無法使用該站點,即使您可以這樣做,因為他們沒有“c:”驅動器。

所以,如果你想存儲文件並允許用戶現在或以后下載它們,我會這樣做(未經測試,它可能會更好,但顯示了概念)......

創建一個代表您的報告目錄的 class

class ReportDirectory{

   static final String path = "./path/to/reports/"; //<-- generic path on your SERVER!

   static{
       //static initializer to make sure directory gets created.  Better ways to do this but this will work!

       File pathAsFile = new File(path).mkdirs()

       if (pathAsFile.exists()){
          println("CREATED REPORT DIRECTORY @ ${pathAsFile.absolutePath}");
       }else{
          println("FAILED TO CREATE REPORT DIRECTORY @ ${pathAsFile.absolutePath}");
       }

   }

   public static File[] listFiles(){
       return new File(path).listFiles(); //<-- maybe use filters to just 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
   }

}

然后在你的 controller 你可以做這樣的事情......

class ReportController{

   def runReport = {
       File report = createReport() //<-- your method to create a 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 the user to download
     }else{
         //handle when the file is not found
     }

   }

   def deleteAllFiles ={
       ReportDirectory.deleteAllFiles()

       [files:ReportDirectory.listFiles()] //<-- return the remaining files, if any.
   }


}

關於這個解決方案的一些評論......

- 這不涉及 MIME 類型,因此瀏覽器將無法確定通過網絡傳輸的二進制數據類型。

這有幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM