Primefaces-在多个文件上传组件中按字母顺序获取文件 [英] Primefaces - Get files in alphabetical order in multiple file upload component
问题描述
我在应用程序中使用 Primefaces多文件上传组件.在这里,我选择"n"个文件,然后单击上传"按钮.然后我需要根据字母顺序在fileUploadListener
中获取每个文件.怎么可能?
I am using Primefaces Multiple file upload component in an application. Here i choose 'n' number of files and clicked on upload button. Then i need to get each files in fileUploadListener
according to alphabetical order. How it possible?
推荐答案
As the multiple file upload component is a jQuery-File-Upload plugin, the default state is not sequential, that means all the files get upload asynchronously.
要使组件进行顺序上载,必须将sequentialUploads
设置为true,并在更改时对当前文件进行一些字母排序.所有这些都是通过javascript完成的.
To get the component to do a sequential upload, you have to set sequentialUploads
to true, and on change we do a little alphabetical sorting of the current files. all this is done by javascript.
假设您的widgetVar为fileUploadWV
Assuming your widgetVar is fileUploadWV
<p:fileUpload widgetVar="fileUploadWV"
fileUploadListener="#{attachmentBean.onUpload}" />
<script>
$(function() {
// setTimeout waits till the widgetVar is ready!
setTimeout(sortFileUpload, 2000);
});
function sortFileUpload() {
//Set this option to true to issue all file upload requests in a sequential order instead of simultaneous requests.
PF('fileUploadWV').jq.data().blueimpFileupload.options.sequentialUploads = true;
//every time a new file is added, sort the files based on name
PF('fileUploadWV').jq.change(function() {
PF('fileUploadWV').files.sort(function fileSort(a, b) {
return a.name.localeCompare(b.name)
})
});
}
</script>
因此,在这种情况下,您的文件将按字母顺序上传.
So in this scenario your files would get uploaded in an alphabetical order.
注意:如果您未将sequenceUploads设置为true,则无法控制要先发送哪个文件.
Note: if you don't set sequentialUploads into true, you have no control which file is going to be sent first.
希望这会有所帮助.
这篇关于Primefaces-在多个文件上传组件中按字母顺序获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!