marcusstarnes
marcusstarnes

Reputation: 6531

How to get BlueImp jQuery FileUpload to generate unique filenames

I currently have an ASP.NET MVC project that is using BlueImp jQuery FileUploader which works great.

https://github.com/blueimp/jQuery-File-Upload

However, it appears that the default behaviour is to use the original filenames when uploading files to the server. Ideally, what I would like is to have the jQuery FileUploader generate a unique filename for each file uploaded.

I did try performing a rename of each uploaded file on the server, but I then realised that the FileUploader seems to hang on to the original filenames.

Is there a way of making the jQuery uploader generate a random/unique filename for each image uploaded (whether that be individually or as a batch)?

Upvotes: 4

Views: 7362

Answers (3)

Paul
Paul

Reputation: 11746

I just had the same issue. Using the upload.class file for PHP I added a unique file name to the handle_file_upload method like so:

before:

$file->name = $this->trim_file_name($name, $type, $index);

after:

$file->name = $this->trim_file_name(md5($name), $type, $index);

I'm sure you can do something similar in ASP.NET


EDIT:

In the latest version it is on line 506 in UploadHandler.php, change:

$name = $this->trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range);

To

$name = $this->trim_file_name($file_path, md5($name), $size, $type, $error, $index, $content_range);

Works perfect!

Upvotes: 13

Jason Mayo
Jason Mayo

Reputation: 356

I edited Paul's answer but it got rejected... Anyway, his answer is correct but the line of code is for an old file.

In the latest version it is on line 506 in UploadHandler.php, change:

 $name = $this->trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range);

To

 $name = $this->trim_file_name($file_path, md5($name), $size, $type, $error, $index, $content_range);

Works perfect!

Upvotes: 0

Jean
Jean

Reputation: 11

  protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
        $index = null, $content_range = null) {
            $file = new stdClass();
 }

before:

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

after:

$file->name = $this->get_file_name(uniqid(), $type, $index, $content_range);

Upvotes: 1

Related Questions