Dropzone.js - 如何在上传到文件夹之前更改文件名

Mr.*_*ppy 17 jquery image-uploading dropzone.js

我正在使用DropzoneJS脚本通过拖放来上传图像,但现在我正在寻找一种解决方案,以便在上传到服务器文件夹之前如何添加带有文件名的当前时间戳,因为我无法上传相同的图像,如果文件已存在于该文件夹中.

我也在下面提到了stackoverflow链接,但我很困惑在哪里实现它.

  1. /sf/answers/1666384191/
  2. /sf/answers/1360291201/

这是dropzone.js脚本供参考

Raj*_*nth 23

请检查我使用PHP实现的以下代码.

索引文件中使用以下代码

$(document).ready(function() {
            Dropzone.autoDiscover = false;
            var fileList = new Array;
            var i =0;
            $("#some-dropzone").dropzone({
                addRemoveLinks: true,
                init: function() {

                    // Hack: Add the dropzone class to the element
                    $(this.element).addClass("dropzone");

                    this.on("success", function(file, serverFileName) {
                        fileList[i] = {"serverFileName" : serverFileName, "fileName" : file.name,"fileId" : i };
                        //console.log(fileList);
                        i++;

                    });
                    this.on("removedfile", function(file) {
                        var rmvFile = "";
                        for(f=0;f<fileList.length;f++){

                            if(fileList[f].fileName == file.name)
                            {
                                rmvFile = fileList[f].serverFileName;

                            }

                        }

                        if (rmvFile){
                            $.ajax({
                                url: "http://localhost/dropzone/sample/delete_temp_files.php",
                                type: "POST",
                                data: { "fileList" : rmvFile }
                            });
                        }
                    });

                },
                url: "http://localhost/dropzone/sample/upload.php"
            });

        });
Run Code Online (Sandbox Code Playgroud)

upload.php的

<?php
$ds = DIRECTORY_SEPARATOR;  // Store directory separator (DIRECTORY_SEPARATOR) to a simple variable. This is just a personal preference as we hate to type long variable name.
$storeFolder = 'uploads';   // Declare a variable for destination folder.
if (!empty($_FILES)) {

    $tempFile = $_FILES['file']['tmp_name'];          // If file is sent to the page, store the file object to a temporary variable.
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  // Create the absolute path of the destination folder.
    // Adding timestamp with image's name so that files with same name can be uploaded easily.
    $date = new DateTime();
    $newFileName = $date->getTimestamp().$_FILES['file']['name'];
    $targetFile =  $targetPath.$newFileName;  // Create the absolute path of the uploaded file destination.
    move_uploaded_file($tempFile,$targetFile); // Move uploaded file to destination.

    echo $newFileName;
}
?>
Run Code Online (Sandbox Code Playgroud)

delete_temp_files.php

<?php
$ds = DIRECTORY_SEPARATOR;  // Store directory separator (DIRECTORY_SEPARATOR) to a simple variable. This is just a personal preference as we hate to type long variable name.
$storeFolder = 'uploads'; 

$fileList = $_POST['fileList'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;


if(isset($fileList)){
    unlink($targetPath.$fileList);
}

?>
Run Code Online (Sandbox Code Playgroud)

希望这对使用ajax上传图像和使用ajax删除有帮助:)

我从以下参考文献中找到:

Dropzone.js-如何从服务器删除文件? Dropzone.js用php删除按钮

还要在第1110行之后的dropzone.js文件中添加以下代码,以防止用户上传具有相同名称的重复文件:)

Dropzone.prototype.addFile = function(file){

Dropzone.prototype.addFile = function(file) {
    if (this.files.length) {
        var _i, _len;
        for (_i = 0, _len = this.files.length; _i < _len; _i++) {
            if(this.files[_i].name === file.name && this.files[_i].size === file.size) {
                return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

参考链接:https://www.bountysource.com/issues/2993843-dropzone-did-not-check-the-duplicate-file-on-addfile?utm_campaign=plugin&utm_content=tracker%2F283989&utm_medium=issues&utm_source=github

  • 这对我来说很完美.谢谢 :) (2认同)

Kal*_*hni 20

要在每次上传之前使用时间戳为文件名添加前缀,您有两个选项,具体取决于您的DropzoneJS版本.

较新版本的DropzoneJS 5.1+确实具有如下使用的renameFile功能:

    ...
    renameFile: function (file) {
        return new Date().getTime() + '_' + file.name;
    }
    ...
Run Code Online (Sandbox Code Playgroud)

旧版本 v4.3 - v5.1中,这有点不同.

在这个版本中有一个renameFilename选项,这是这样使用的:

Dropzone.autoDiscover = false;
$(document).ready(function () {
    $(".dropzone").dropzone({
        renameFilename: function (filename) {
            return new Date().getTime() + '_' + filename;
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

快乐的编码,卡拉施

  • 对于任何在 5.1+ 版本上苦苦挣扎的人,这里的代码是不正确的。它需要是: `renameFile: function (file) { return new Date().getTime() + '_' + file.name; }` (3认同)