2016-09-22 9 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

ありがとうございます@プラカシ – Sagar

+0

私の答えがあなたの問題を解決した場合は、緑色のティックをクリックしてそれを受け入れることができますまた、上向きの矢印をクリックしてupvoteすることができます。 –

関連する問題