2016-09-22 95 views
-2

我有一個名爲Product的域類。我已將這些信息和圖像保存在數據庫中。但在檢索信息時,我檢索了所有信息,但無法檢索圖像。從grails中檢索數據庫中的信息和圖像

Domain Class

class Product { 

     String productName  
     String description  
     int price  
     byte [] photo 
     String phototype 
    } 

I have saved the information and image in database using this action in ProductController. In gsp page, I used <g:uploadForm> tag to get information. 



def saveProduct(){ 

    def pic = request.getFile('picture') 
    product.properties['productName','description','price'] = params 
    product.photo=pic.bytes 
    product.phototype=pic.contentType 

    if(!product.save()){ 
     render (view: "/adminPanel", model: [upload: "Product Failed to Upload",product:product]) 
     return 
    } 
    else { 
     render (view: "/adminPanel", model: [upload: "Product Successfully Saved!!",product: product]) 
    } 
} 

這段代碼保存在數據庫中的信息和圖像。現在,我如何在adminPanel.gsp頁面中顯示圖像和產品信息?我應該在控制器和gsp頁面上寫什麼樣的代碼?

回答

0

嘗試使用插件。
Avatar uploader是一個很好的。

簡單的頭像上傳 - Grails使這些圖片的上傳和顯示幾乎微不足道。

+0

謝謝@http:/ /stackoverflow.com/users/1233197/mikelis-baltruks Sir – Sagar

+0

@Sagar如果您認爲我的回答對您的問題有好處,請點擊右側的複選標記接受它。 –

0

那麼你可以很簡單地顯示你的圖像存儲在DB

只需添加一個動作,用於從表中的圖像,並將其發送到GSP

class ProductController { 

    def saveProduct(){ 
    ....... 
    }  

    /** Action for fetching the image byte array 
     Assuming that you have passed the "productName" from UI 
     to fetch the particular product image 
    */ 
    def fetchProductImage(){ 
     def product = Product.findByProductName(params.productName) 
     byte[] imageInByte = product.photo 
     response.contentType = 'image/png' // or the appropriate image content type 
     response.outputStream << imageInByte 
     response.outputStream.flush() 
    }   
} 

並給予調用此控制器操作從GSP View象下面這樣:

<img src="${createLink(controller: 'product', action: 'fetchProductImage', params: ['productName': 'Some_product_name'])}"/> 
+0

謝謝@Prakash爵士 – Sagar

+0

如果我的回答已經解決了您的問題,那麼您可以通過點擊綠色的勾號來接受它,並且您也可以通過點擊向上箭頭來對其進行修改。 –