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

前端之家收集整理的这篇文章主要介绍了jquery – Dropzone.js – 上传到文件夹之前如何更改文件名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 DropzoneJS脚本来上传图片,拖放&但现在我正在寻找一个解决方案,以便在上传到服务器文件夹之前如何添加具有文件名称的当前时间戳,因为如果该文件已经存在于文件夹中,则无法上传相同的图像.

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

> https://stackoverflow.com/a/23805488/3113858
> https://stackoverflow.com/a/19432731/3113858

这是dropzone.js script参考

解决方法

请检查使用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"
            });

        });

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;
}
?>

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

?>

希望这将有助于使用ajax上传图像并使用ajax删除:)

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

Dropzone.js- How to delete files from server?
Dropzone.js remove button with php

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

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

参考链接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

原文链接:https://www.f2er.com/jquery/180121.html

猜你在找的jQuery相关文章