我正在使用<rich:fileUpload />
从 RichFaces4 可以看到 here 。
但正如你所看到的,用户可以选择很多图片,但我希望他只能选择一张图片。
上传完成后,如何调用托管 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>
最佳答案
RF 3.3 曾支持此功能。但为了节省 RF 4.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 在 RF 默认脚本(已包含 jQuery)之后加载。您可以通过 <h:outputScript>
来做到这一点在正文中,如有必要,您可以使用 target="head"
设置.
此CSS限制文件列表的高度并删除单个项目的底部边框:
.rf-fu-lst {
height: 60px;
}
.rf-fu-itm {
border: 0;
}
确保上述 CSS 在 RF 默认样式之后加载。您可以通过 <h:outputStylesheet>
来做到这一点在体内(所以不要放入头部)。
And how do I call a method in my managed bean (which send the image to my Amazon S3 bucket) after the upload has been completed ?
只需在 fileUploadListener
附加的监听器方法中执行此操作即可属性。
<rich:fileUpload fileUploadListener="#{bean.upload}">
关于jsf - 如何限制RichFaces 4丰富:fileUpload to single file upload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8417756/