最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get BlueImp jQuery FileUpload to generate unique filenames - Stack Overflow

programmeradmin4浏览0评论

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

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

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

https://github./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)?

Share Improve this question asked Jun 27, 2012 at 18:21 marcusstarnesmarcusstarnes 6,53115 gold badges70 silver badges114 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 13

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!

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

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!

发布评论

评论列表(0)

  1. 暂无评论