简体   繁体   English

如何在上传文件之前以数字方式重命名Plupload中的文件?

[英]How do I numerically rename files in Plupload before they're uploaded?

I am using the Plupload jQuery UI widget as a basis for my uploader. 我使用Plupload jQuery UI小部件作为上载程序的基础。 The widget allows the user to drag reorder the added files. 该小部件允许用户拖动重新排序添加的文件。

Upload is begun automatically upon the submission of a form, not manually by the user. 上传是在提交表单后自动开始的,而不是由用户手动开始的。 During the upload, the UI is blocked so the user can't add anymore files. 在上载期间,UI被阻止,因此用户无法再添加文件。

Before the upload begins, I need to rename all of the files to upload numerically so that the first file to be uploaded (the one at the top of the jQuery UI widget list) is '1', the second '2' and so on. 在开始上传之前,我需要重命名所有文件以数字方式进行上传,以便要上传的第一个文件(jQuery UI小部件列表顶部的一个)为“ 1”,第二个为“ 2”,依此类推。 。

For instance, given the following list: 例如,给出以下列表:

bob.jpg
ann.jpg
doug.jpg
chris.jpg

I want to have them renamed: 我想将它们重命名:

1.jpg
2.jpg
3.jpg
4.jpg

This works when the user adds new files or removes existing files. 当用户添加新文件或删除现有文件时,此方法有效。 What doesn't work is when the user drag reorders the files . 不起作用的是当用户拖动文件时重新排序 Here is my instantiation code for the uploader: 这是我的上载程序的实例化代码:

$('#' + div_id).plupload({
  runtimes: 'html5, flash, silverlight',
  url: 'upload',
  unique_names: false,
  rename: true,
  sortable: true,
  buttons: { browse: true, start: false, stop: false },      

  // Flash settings
  flash_swf_url: 'js/plupload/plupload.flash.swf',

  // Silverlight settings
  silverlight_xap_url : 'js/plupload/plupload.silverlight.xap',

 init: {
   QueueChanged: function(up) {
     for (var i = 0; i < up.files.length; i++) {
       up.files[i].name = (i+1);
     }
   }
 }
});

I can't figure out how to detect when the user drag reorders. 我不知道如何检测用户拖动重新排序的时间。 Failing that, I don't see an event that fires when the upload of the queue first begins. 失败的话,我看不到第一次开始上传队列时触发的事件。

In case anyone else comes across this problem: 万一其他人遇到这个问题:

Instead of using the QueueChanged() event I simply used jQuery to get every Plupload instance and within jQuery's .each() loop I used this code: 我没有使用QueueChanged()事件,而是仅使用jQuery获取每个Plupload实例,并且在jQuery的.each()循环中,我使用了以下代码:

var regex = /(?:\.([^.]+))?$/;
for (var i = 0; i < uploader.files.length; i++) {
  var ext = regex.exec(uploader.files[i].name)[1];
  uploader.files[i].name = (ext == undefined) ? (i+1) : (i+1) + '.' + ext;
}
uploader.start();

This way I don't do any renaming until immediately before uploading. 这样,直到上传之前,我才进行任何重命名。 It also has the added advantage of not renaming the files in the UI widget before upload (this avoiding some confusion to the end user). 它还具有在上传之前不重命名UI小部件中文件的附加优点(这避免了对最终用户的混淆)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM