2013-07-09 31 views
0

我是Grails中的新成員。我正在使用uploadr plugin上傳圖片。上傳圖片效果很好。我的圖片已成功上傳到我的目錄中。如何在Grails中顯示uploadr插件上傳的圖片

現在我想在show.gsp文件中顯示這些圖像。但我對此毫不知情。 這是我uploadr標籤:

<uploadr:add name="fileupload" path="C:/Users/Shreshtt/workspace/groovypublish/grails-app/uploader" direction="up" maxVisible="8" unsupported="/uploadr/upload/warning" rating="true" voting="true" colorPicker="true" maxSize="0" /> 

看到這個demo也..

我怎麼能在我看來,頁面顯示這些圖像也。請幫忙。

回答

0

你應該在你的應用程序中使用的路徑是這樣的:

<uploadr:add name="ComonUp10" path="uploadFile/" direction="up" maxVisible="8" unsupported="/my/controller/action" rating="true" voting="true" colorPicker="true" maxSize="204800000" /> 

這裏uploadFile /會在你的Grails應用程序網絡應用程序目錄中的文件夾。

,然後你就可以看到已經上傳的文件爲:

<% def path = new File("uploadFile/") %> 
<uploadr:add name="mySecondUploadr" path="${path}" direction="up" maxVisible="5" unsupported="${createLink(plugin: 'uploadr', controller: 'upload', action: 'warning')}"> 
<% path.listFiles().each { file -> %> 
    <uploadr:file name="${file.name}"> 
     <uploadr:fileSize>${file.size()}</uploadr:fileSize> 
     <uploadr:fileModified>${file.lastModified()}</uploadr:fileModified> 
     <uploadr:fileId>myId-${RandomStringUtils.random(32, true, true)}</uploadr:fileId> 
    </uploadr:file> 
<% } %> 
</uploadr:add> 

而對於顯示在該目錄中上傳的圖像,而不uploadr,您可以在GSP頁面作爲添加圖像標籤:

<img class="" src="${resource(dir:'uploadFile',file:'your-image.jpg')}" /> 

您可以在每個循環中添加圖像,並讓他們一個一個我喜歡:

<g:each in="imageContainingList" var="oneRowObject"> 
    <img class="" src='${resource(dir:"uploadFile", file:"${oneRowObject.imageFile}.jpg")}' /> 

</g:each> 

或者,如果要列出uploadFile文件夾中的所有圖像,則可以嘗試:

您必須收集控制器中的所有圖像名稱,將它們傳遞到視圖並使用grails圖像標記顯示它們。

爲了收集你需要你的Grails應用程序路徑中的所有圖像名稱:

def showImageGalleryControllerFunction(){ 
    def imageDir = grailsAttributes.getApplicationContext().getResource("/uploadFile/").getFile().toString() 

    //Collect image names 

    def images = [] 
    new File(imageDir).eachFileMatch (~/.*.png/) { file -> 
     images << file.getName() 
    } 

    [images:images] 
} 

,並在您showImageGalleryControllerFunction.gsp文件:

<g:each in="${images}" var="image"> 
    <g:img dir="images" file="$image" /> 
</g:each> 
+0

在這個例子中不使用任何圖像標籤? –

+0

我想製作一個圖片庫,其中所有上傳的圖片都必須顯示。我的圖像正確保存在uploadfile目錄中。現在我想要將所有圖像提取到我的圖像庫中 –

+0

我已經用圖像標記更新了答案 –