tom_cruz
tom_cruz

Reputation: 411

Jquery BlueImp how to change filename before upload?

Is it possible to change the filename of the file being uploaded via jquery blueimp?

Upvotes: 6

Views: 11946

Answers (6)

Maave
Maave

Reputation: 138

Here's a client-side solution specific to BlueImp. It uses BlueImp's "add" callback. My example will strip commas from the filename.

$('#fileupload').fileupload({
    add: function (e, data) {
        $.each(data.files, function (index, file) {
            //set new name here
            var newname = data.files[index].name.replace(/,/g, '');
            Object.defineProperty(data.files[index], 'name', {
                value: newname
            });
        });
        $.blueimp.fileupload.prototype.options.add.call(this, e, data);
    }
})

Upvotes: 3

newbie.user88
newbie.user88

Reputation: 69

  1. Open UploadHandler.php

  2. Find

    protected function handle_file_upload();

  3. Edit

$file->name = $this->get_file_name($uploaded_file, 'GiveYourNewName', $size, $type, $error, $index, $content_range);

Upvotes: 0

150Years
150Years

Reputation: 101

The name property of File objects is read only, but an alternative name can be provided as uploadName property for each individual file.

Can use callback

$('#fileupload').fileupload
  submit: (e, data) ->
    data.files[0].uploadName = 'newname'

If your data.files contains all files in one time you can iterate through them.

Upvotes: 0

Vladimir Salguero
Vladimir Salguero

Reputation: 5947

require('UploadHandler.php');
class CustomUploadHandler extends UploadHandler {
    protected function trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range) {
            $name = 'First_' . microtime(true);
            $name = str_replace('.', '', $name);
            return $name;
        }
}

$upload_handler = new CustomUploadHandler();

In your custom file index.php initialize this function of UploadHandler.php

Upvotes: 17

Saymon
Saymon

Reputation: 520

This can be achieved with only change the trim_file_name() function to the following in the UploadHandler.php file.

protected function trim_file_name(VARIABLES) {
    $name = uniqid();
    return $name;
}

and that's all

Upvotes: 4

danteinzu
danteinzu

Reputation: 82

This solution was posted by 'lethal.industry' user here. You must paste this code inside of UploadHandler class in UploadHandler.php file

   //custom function which generates a unique filename based on current time
    protected function generate_unique_filename($filename = "")
        {

            $extension = "";
            if ( $filename != "" )
            {
                $extension = pathinfo($filename , PATHINFO_EXTENSION);

                if ( $extension != "" )
                {
                    $extension = "." . $extension;
                }
            }

            return md5(date('Y-m-d H:i:s:u')) . $extension;
        }

You can modify the filename as you like change the last line 'return...'

Then you search the 'handle_file_upload' function in UploadHandler.php and replace this line

$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);

for this line

$file->name = $this->generate_unique_filename($name);

Upvotes: -1

Related Questions