2013-01-11 84 views

回答

3

域類

class Data { 
    byte[] pdfFile 

    static mapping = { 
     pdfFile sqlType:'longblob'  //use mysql 
    } 

    static constraints = { 
     pdfFile nullable:true 
    } 
} 

GSP視圖提交URL的控制器,即getFile.gsp例如:

<g:form url="[action:'savePdf',controller:'data']" > 
     <g:textField name="externalUrl" > 
     <g:submitButton name="submit" value="Submit" /> 
</g:form> 

DataController類

class DataController { 
    def savePdf() {         //save pdf file into database 
     def url = params.externalUrl   // for example:'http://moodle.njit.edu/tutorials/downloading_moodle.pdf' 

     def localFile = new FileOutputStream('test.pdf') 
     localFile << new URL(url).openStream() 
     localFile.close() 

     def pdfFile = new FileInputStream('test.pdf') 
     byte[] buf = new byte [102400] 
     byte[] pdfData = new byte[10240000]    //pdf file size limited to 10M 
     int len = pdfFile.read(buf, 0, 102400) 
     ByteArrayOutputStream bytestream = new ByteArrayOutputStream() 
     while(len > 0) { 
      bytestream.write(buf, 0, len) 
      len =pdfFile.read(buf, 0, 102400) 
     } 
     data.pdfFile = bytestream.toByteArray() 
     data.save() 
    } 

    def renderPdf() {        //for pdf file download 
     def dataInstance = Data.get(params.id) 
     response.setContentType('application/pdf') 
     byte[] pdf = dataInstance?.pdfFile 
     response.outputStream << pdf 
    } 
} 

要觸發renderPdf()方法,把一個鏈接在另一個GSP視圖,讓我們說render.gsp

刪除或已回答的問題
<a href="${createLink(uri:'/data/renderPdf/'+dataInstance.id)}">pdf file</a> 
+0

嗨,thanx回覆,你告訴我只有pdf如何存儲到數據庫。我只想將pdf的內容存儲到數據庫中......假設數據以pdf格式表格格式 – itsvks

+0

那麼,那可能是一個整體項目。在這裏我不期待任何關於這樣一個重大話題的答案。 – coderLMN

+0

嘿,你可以給一些想法..如何解決這個問題..我應該使用哪個庫? – itsvks

相關問題