Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 611
marcusstarnes Avatar asked Dec 09 '22 23:12

marcusstarnes


2 Answers

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!

like image 75
Paul Avatar answered May 07 '23 03:05

Paul


  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);
like image 26
Jean Avatar answered May 07 '23 05:05

Jean