Primefaces - 在多文件上传组件中按字母顺序获取文件 [英] Primefaces - Get files in alphabetical order in multiple file upload component

查看:19
本文介绍了Primefaces - 在多文件上传组件中按字母顺序获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用

希望这会有所帮助.

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.

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.

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.

Note: if you don't set sequentialUploads into true, you have no control which file is going to be sent first.

Github, Online Demo

Hope this helps.

这篇关于Primefaces - 在多文件上传组件中按字母顺序获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆