我想知道是否可以,通过使用primefaces提前上传模式来限制用户只上传一个文件,目前我有:
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
multiple="false"
update="messages"
sizeLimit="100000000"
allowTypes="/(.|/)(gif|jpe?g|png|doc|docx|txt|pdf)$/"
auto="false"/>
<p:growl id="messages" showDetail="true"/>
正如你所看到的,我有muliple="false",但用户仍然可以上传多个文件,有什么提示吗?
编辑:
<p:fileUpload widgetVar="upload" fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
multiple="false"
update="messages"
label="Select File"
sizeLimit="100000000"
allowTypes="/(.|/)(gif|jpe?g|png|doc|docx|txt|pdf|html)$/"
auto="false"/>
<p:growl id="messages" showDetail="true"/>
已经在之上添加了widgetVar
在我的js 中
<script type="text/javascript">
function Naviagtion()
{
//alert("Sent to the printing holding queue, you may close this app now, your work will still print out ");
window.setTimeout(afterDelay, 500);
location.href = 'FilesUploaded.xhtml';
}
upload.buttonBar.find('input[type=file]').change(function() {
if (this.value) {
var files = upload.uploadContent.find('.files tr');
if (files.length > 1) {
files.get(0).remove();
}
}
});
</script>
但我仍然可以多次上传,我是不是朝着正确的方向去做
虽然解决它的更好行为应该是@BalusC建议的,但在primefaces 4.0中,我看到了属性
fileLimit="1"
您可以使用"选择"按钮将其设置为1以禁止添加多个文件。当用户添加更多文件时,它只是简单地说
"超过最大文件数"
multiple="false"
仅告诉Web浏览器在特定于浏览器的浏览对话框中禁用多个文件选择。然而,这并不能阻止最终用户多次点击PrimeFaces文件上传部分的Choose按钮来多次浏览和添加单个文件。
您最好的选择是在选择新文件时引入一些JS/jQuery来删除所有以前选择的文件。如果你已经给你的<p:fileUpload>
一个widgetVar="upload"
,那么这应该是:
$(document).ready(function() {
upload.buttonBar.find('input[type=file]').change(function() {
if (this.value) {
var files = upload.uploadContent.find('.files tr');
if (files.length > 1) {
files.get(0).remove();
}
}
});
});
适用于PrimeFaces 3.5。
如果您将文件限制设置为1,并且在加载文件时发生了一些错误,则必须刷新页面才能再次上传文件。如果你不刷新页面,你会收到超出限制的错误消息。
我使用JS的解决方案,就像在公认的答案中一样,但必须更改选择器,因为wigetWar对我不起作用
在我看来,我有:
<p:fileUpload id="objectUpload"... />
在我的portlet主题中,带有文件的表有一个css类"ui-fileupload-files"。
$(document).ready(function() {
$("div[id*='objectUpload']").find('input[type=file]').change(function() {
if (this.value) {
var files = $("div[id*='objectUpload']").find('.uifileupload-files tr');
if (files.length > 1) {
files.get(0).remove();
}
}
});
});
希望能有所帮助。我使用PrimeFaces 6.0