Plupload file rename

ibininja picture ibininja · Dec 18, 2012 · Viewed 7.7k times · Source

I am using Plupload to upload to S3; My problem is that I want to change names of files, so when they reside in S3 they will be changed to a format I want. I managed to retrieve the file name of files uploaded by the function:

 FilesAdded: function (up, files) {
                    for (var i in files) {
                        files[i].name = files[i].name.split('_').join(' ').trim();
                        alert('Selected files: ' + files[i].name);

                    }

the file name changes in the control but when I check the S3 the file is unchanged.

I made sure unique_names property is false and rename property to true; but did not work any help?

Answer

droider picture droider · Feb 23, 2014

I faced the same problem i.e. using pluploader for S3 and wanted to normalize file names before uploading. Unfortunately 'Files' param appears to be read-only and any changes to file's name doesn't show up in the submitted form.

But if we change the 'key' param to an exact string (normalized name) it will cause S3 to save it with a different name. We can create a normalized name and use it in the 'key' param in the multipart_param in the 'FileAdded' callback.

....
FilesAdded: function(up, files) {
  console.log("Uploader.FilesAdded ", up, files);

  var file = files[0];

  //Replace unwanted characters with "_"
  var new_name = file.name.replace(/[^a-z0-9\.]+/gi,"_").toLowerCase();

  console.log("Changing file name to ", file.name);

  //Create multipart_params here (or just set the 'key' and 'Filename')
  var multipart_params =  {
    'key': config.key + new_name, // *instead of ${filename}
    'Filename':  config.key + new_name,
    'acl': config.acl,
    'Content-Type': '',
    'AWSAccessKeyId' : config.accessId,
    'policy': config.policy,
    'signature': config.signature
  };

 //
 up.settings.multipart_params = multipart_params;
}
....