2011-12-07 47 views
4

我使用RichFaces4的<rich:fileUpload />,如您所見here。 但正如你所看到的那樣,用戶可以選擇很多圖片,但我希望他可以選擇一張圖片。如何限制RichFaces 4 rich:fileUpload單個文件上傳?

上傳完成後,如何在我的託管bean(將映像發送到Amazon S3存儲桶)中調用方法?

編輯:

<ui:composition> 
<h:outputStylesheet> 
    .top { 
     vertical-align: top; 
    } 

    .info { 
     height: 202px; 
     overflow: auto; 
    } 

    .rf-fu-lst { 
     height: 60px; 
    } 

    .rf-fu-itm { 
     border: 0; 
    } 
</h:outputStylesheet> 
<h:outputScript target="head"> 
jQuery.extend(RichFaces.ui.FileUpload.prototype, { 
    __updateButtons: function() { 
     if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) { 
      if (this.items.length) { 
       this.uploadButton.css("display", "inline-block"); 
       this.addButton.hide(); 
      } else { 
       this.uploadButton.hide(); 
       this.addButton.css("display", "inline-block"); 
      } 
     } else { 
      this.uploadButton.hide(); 
      this.addButton.css("display", "inline-block"); 
     } 
    } 
}); 
</h:outputScript> 

    <h:form id="form_user_upload_picture" > 
     <h:panelGrid columns="2" columnClasses="top, top"> 
      <rich:fileUpload 
       id="upload" 
       fileUploadListener="#{user_file_upload.listener}" 
       acceptedTypes="jpg, gif, png, bmp" 
       addLabel="Adicionar" 
       clearAllLabel="Limpar todas" 
       clearLabel="limpar" 
       deleteLabel="apagar" 
       doneLabel="upload realizado" 
       sizeExceededLabel="arquivo muito grande, tente um arquivo de tamanho menor" 
       serverErrorLabel="ocorreu um erro em nosso servidor, tente novamente por favor" 
       uploadLabel="Enviar"> 

       <a4j:ajax event="uploadcomplete" execute="@none" render="picture" /> 
      </rich:fileUpload> 
     </h:panelGrid> 

    </h:form> 
</ui:composition> 

回答

4

這是用來在RF 3.3的支持。但爲了節省RF4.0移植的時間,其中<rich:fileUpload>得不到應有的重視。目前有很多open tickets來改善它。

在他們改進之前,您目前最好的選擇是引入一些自定義的jQuery/JS和CSS來實現選擇和上傳只有一個文件的功能需求。

該腳本可以防止最終用戶通過隱藏的添加按鈕上傳多個文件時,已經是選擇了一個文件:

jQuery.extend(RichFaces.ui.FileUpload.prototype, { 
    __updateButtons: function() { 
     if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) { 
      if (this.items.length) { 
       this.uploadButton.css("display", "inline-block"); 
       this.addButton.hide(); 
      } else { 
       this.uploadButton.hide(); 
       this.addButton.css("display", "inline-block"); 
      } 
     } else { 
      this.uploadButton.hide(); 
      this.addButton.css("display", "inline-block"); 
     } 
    } 
}); 

確保上述JS加載射頻默認腳本(包括jQuery已經)。你可以在體內使用<h:outputScript>做到這一點,如果需要,可以用target="head"來設置。

這限制了CSS文件列表的高度和刪除單個項目的底部邊框:

.rf-fu-lst { 
    height: 60px; 
} 
.rf-fu-itm { 
    border: 0; 
} 

確保上述CSS之後射頻默認樣式加載。你可以通過體內的<h:outputStylesheet>來做到這一點(所以不要放在頭部)。


,我如何調用一個方法在我管理的bean後上傳(將圖像發送到我的亞馬遜S3鬥)已經完成?

只是在附加到fileUploadListener屬性的偵聽器方法中完成這項工作。

<rich:fileUpload fileUploadListener="#{bean.upload}"> 
+0

我上傳到S3後,文件上傳完成後,謝謝。 但是現在我試圖只上傳一個文件,正如你所說的,但給我一個錯誤:實體名稱必須緊跟在實體引用中的'&'後面。 –

+0

你不應該把JavaScript代碼放在一個XML文件中,而是放在一個JS文件中:)用'&'替換'&'。 – BalusC

+0

它適合隊友,謝謝! –