简体   繁体   English

如何获取BlueImp jQuery FileUpload以生成唯一的文件名

[英]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. 我目前有一个使用BlueImp jQuery FileUploader的ASP.NET MVC项目,效果很好。

https://github.com/blueimp/jQuery-File-Upload 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. 理想情况下,我想要的是让jQuery FileUploader为每个上传的文件生成唯一的文件名。

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. 我确实尝试对服务器上的每个上传文件进行重命名,但后来我意识到FileUploader似乎挂起了原始文件名。

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)? 有没有办法让jQuery上传器为每个上传的图像生成一个随机/唯一的文件名(无论是单独还是批量)?

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: 使用PHP的upload.class文件,我为handle_file_upload方法添加了一个唯一的文件名,如下所示:

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 我相信你可以在ASP.NET中做类似的事情


EDIT: 编辑:

In the latest version it is on line 506 in UploadHandler.php, change: 在最新版本中,它位于UploadHandler.php的第506行,更改:

$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. 我编辑了Paul的答案,但它被拒绝了......无论如何,他的答案是正确的,但代码行是针对旧文件的。

In the latest version it is on line 506 in UploadHandler.php, change: 在最新版本中,它位于UploadHandler.php的第506行,更改:

 $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! 作品完美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM