如何更改dropzone.js中的默认文本?


74

我正在使用dropzone.js上载文件。但是,我很难更改默认文本。

我尝试实例化dropzone类:

$(document).ready(function(){
  $(".foo").dropzone({ dictDefaultMessage: "hello" });
});

有了这个标记:

    <div class="span4">
      <form action="http://localhost/post" method="post" accept-charset="utf-8" id="drop3" class="foo" enctype="multipart/form-data"> </form>
    </div>
    <div class="span4">
      <form action="http://localhost/post" method="post" accept-charset="utf-8" id="drop4" class="foo" enctype="multipart/form-data"> </form>
  </div>

当然,这使我能够上传文件,但是默认文本为空白。

我测试了以下内容:

 $(".foo").dropzone();

我似乎得到了相同的结果-没有默认文字。所以..如何更改默认文本?

Answers:


176

在您的dropzone表单中添加一个元素,如下所示:

<div class="dz-message" data-dz-message><span>Your Custom Message</span></div>

5
我可以确认这有效,同时将“ dictDefaultMessage”更改为设置似乎无济于事。这主要是因为文本被替换为带有文本的图像。
Florian Rachor 2015年

1
这项工作有效,但是对所有其他类型的消息有什么作用?文档中有一个很大的漏洞
T型Moty 2016年

这是可行的,但是,这迫使我仅单击文本以显示fileupload对话框。现在,在文本外部单击将显示文件
上传

我正在使用<dropzone> html标记,并且没有其他选择,因为我在角度项目中。将上述内容放在其中不起作用。
tatsu

72

您可以使用以下方法更改所有默认消息

Dropzone.prototype.defaultOptions.dictDefaultMessage = "Drop files here to upload";
Dropzone.prototype.defaultOptions.dictFallbackMessage = "Your browser does not support drag'n'drop file uploads.";
Dropzone.prototype.defaultOptions.dictFallbackText = "Please use the fallback form below to upload your files like in the olden days.";
Dropzone.prototype.defaultOptions.dictFileTooBig = "File is too big ({{filesize}}MiB). Max filesize: {{maxFilesize}}MiB.";
Dropzone.prototype.defaultOptions.dictInvalidFileType = "You can't upload files of this type.";
Dropzone.prototype.defaultOptions.dictResponseError = "Server responded with {{statusCode}} code.";
Dropzone.prototype.defaultOptions.dictCancelUpload = "Cancel upload";
Dropzone.prototype.defaultOptions.dictCancelUploadConfirmation = "Are you sure you want to cancel this upload?";
Dropzone.prototype.defaultOptions.dictRemoveFile = "Remove file";
Dropzone.prototype.defaultOptions.dictMaxFilesExceeded = "You can not upload any more files.";

5
在此处查看所有消息键的列表:dropzonejs.com/#config-dictDefaultMessage
tuomassalo

42

创建放置区时,您可以设置默认消息,如下所示。

var dropzone = new Dropzone("form.dropzone", {
   dictDefaultMessage: "Put your custom message here"
});

然后

$('div.dz-default.dz-message > span').show(); // Show message span
$('div.dz-default.dz-message').css({'opacity':1, 'background-image': 'none'});

10

此文本位于dropzone的默认配置中,您可以像这样覆盖:

Dropzone.prototype.defaultOptions.dictDefaultMessage = "Your text";

1
谢谢!我确实不需要更改该消息,但这是唯一帮助我更改defaultOptions的内容
Silvestre

9

首先在表单中添加一个ID,例如mydz,然后添加以下js:

Dropzone.options.mydz = {
    dictDefaultMessage: "your custom message"
};

整个页面(在这种情况下为index.php):

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<script src="dropzone.js"></script>
<link rel="stylesheet" type="text/css" href="./dropzone.css">
<title></title>

</head>

<body>

<form action="upload.php" class="dropzone" id="mydz"></form>
<script type="text/javascript">

Dropzone.options.mydz = {
    dictDefaultMessage: "Put your custom message here"
};


</script>

</body>
</html>

3
这就是文档所说的,但不幸的是,它对我不起作用
Victor Laerte 2015年

1
您是否在表单中添加了ID?如果这样做,它将无法正常工作。将dropzone.js添加到页面顶部,并将我给您的JS添加到页面底部,它必须可以工作。
Ollicca Mindstorm

我刚刚用整个页面更新了我的帖子,复制了粘贴内容,包括相关文件,然后查看它是否有效。
Ollicca Mindstorm

7
myDropzonePhotos = new Dropzone('#dropzone-test',
{
    url                : 'upload_usuario.php?id_usuario=' + id_usuario,
    maxFiles           : 1, 
    thumbnailWidth     : 1200,
    thumbnailHeight    : 300,
    dictDefaultMessage : 'Change the text here!',
    init: function()
    {
     ....

3
嗨!欢迎来到StackOverflow!回答问题时,不仅要提供代码,还请对其进行解释。在此处
ayushgp,2016年

4

我摆弄了几个小时。

由于某些原因,这三件事需要完成:

  1. 我的dropzone标签不能在我使用dropzone的同一页面上。我必须在模板页面上引用它们
  2. 您要变成放置区的元素必须具有一类“放置区”
  3. 您必须在我正在处理的页面的js文件顶部添加以下内容。

Dropzone.autoDiscover = false;

初始化:

var myDropzone = new Dropzone("#id-upload-dropzone", {
    url: "/home/Upload",
    dictDefaultMessage: 'Drop image here (or click) to capture/upload'
});

当我按顺序排列所有3个之后,dictDefaultMessage选项就起作用了。


3

为了在Asp.Net Razor页面中本地化Dropzone,我使用以下方法来避免解码的字符:

为所有消息创建HTML元素

<!-- localization elements -->

<div class="modal" aria-hidden="true">

    <span id="dictDefaultMessage">@_localizer["Drop files here or click to upload."]</span>

    <span id="dictFallbackMessage">@_localizer["Your browser does not support drag'n'drop file uploads."]</span>

    <span id="dictFallbackText">@_localizer["Please use the fallback form below to upload your files like in the olden days."]</span>

    <span id="dictFileTooBig">@_localizer["File is too big ({{filesize}}MiB). Max filesize: {{maxFilesize}}MiB."]</span>

    <span id="dictInvalidFileType">@_localizer["You can't upload files of this type."]</span>

    <span id="dictResponseError">@_localizer["Server responded with {{statusCode}} code."]</span>

    <span id="dictCancelUpload">@_localizer["Cancel upload"]</span>

    <span id="dictCancelUploadConfirmation">@_localizer["Are you sure you want to cancel this upload?"]</span>

    <span id="dictUploadCanceled">@_localizer["Upload canceled."]</span>

    <span id="dictRemoveFile">@_localizer["Delete"]</span>

    <span id="dictRemoveFileConfirmation">@_localizer["Are you sure you want to delete this file?"]</span>

    <span id="dictMaxFilesExceeded">@_localizer["You can not upload any more files."]</span>

    <span id="dictFileSizeUnits_TB">@_localizer["TB"]</span>

    <span id="dictFileSizeUnits_GB">@_localizer["GB"]</span>

    <span id="dictFileSizeUnits_MB">@_localizer["MB"]</span>

    <span id="dictFileSizeUnits_KB">@_localizer["KB"]</span>

    <span id="dictFileSizeUnits_b">@_localizer["b"]</span>

</div>

然后将消息绑定到Dropzone元素:

<script>
// get elements for localization

        with (Dropzone.prototype.defaultOptions) {

            dictDefaultMessage = document.getElementById("dictDefaultMessage").innerText;

            dictFallbackMessage = document.getElementById("dictFallbackMessage").innerText;

            dictFallbackText = document.getElementById("dictFallbackText").innerText;

            dictFileTooBig = document.getElementById("dictFileTooBig").innerText;

            dictInvalidFileType = document.getElementById("dictInvalidFileType").innerText;

            dictResponseError = document.getElementById("dictResponseError").innerText;

            dictCancelUpload = document.getElementById("dictCancelUpload").innerText;

            dictCancelUploadConfirmation = document.getElementById("dictCancelUploadConfirmation").innerText;

            dictUploadCanceled = document.getElementById("dictUploadCanceled").innerText;

            dictRemoveFile = document.getElementById("dictRemoveFile").innerText;

            dictRemoveFileConfirmation = document.getElementById("dictRemoveFileConfirmation").innerText; // if this is null, the user will not be prompted when deleting file.

            dictMaxFilesExceeded = document.getElementById("dictMaxFilesExceeded").innerText;

            dictFileSizeUnits = {

                tb: document.getElementById("dictFileSizeUnits_TB").innerText,

                gb: document.getElementById("dictFileSizeUnits_GB").innerText,

                mb: document.getElementById("dictFileSizeUnits_MB").innerText,

                kb: document.getElementById("dictFileSizeUnits_KB").innerText,

                b: document.getElementById("dictFileSizeUnits_b").innerText

            };

        };

</script>

有关使用Dropzone的完整拖放文件上传示例的信息,请参见以下GitHub存储库:https : //github.com/LazZiya/FileUpload


2

如果您不反对JQuery,则将隐藏默认图像:

$('form.dropzone').find('div.default.message').css('background-image','none');

并且,这将显示默认范围,您可以将其更改为所需的任意值:

$('form.dropzone').find('div.default.message').find('span').show();
$('form.dropzone').find('div.default.message').find('span').empty();
$('form.dropzone').find('div.default.message').find('span').append('Drop files here or click here to upload an image.');

2

在dropzone的CSS中寻找

.dropzone .dz-default.dz-message

在本课程中删除

background-image: url("../images/spritemap.png");

接下来要做的就是搜索此类

.dropzone .dz-default.dz-message span {
  display: none;
}

并将其更改为display:block


2

如果要以编程方式创建Dropzone,则必须按如下所示设置选项:

Dropzone.autoDiscover = false;

profilePicture = new Dropzone('#profile-picture', {
    url: "/uploadPicture.php",

    // if you are using laravel ..., you dont need to put csrf in meta tag
    headers: {
        'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },

    dictDefaultMessage: "Your default message Will work 100%",

    /other options
    paramName: "profile_picture",
    addRemoveLinks: true,
    maxFilesize: 1,
    maxFiles: 10,

    dictRemoveFile: "Remove",

});

如果您以这种方式使用它,它将无法正常工作...

let myDropzone = new Dropzone("#profile-picture", {

    url: "/uploadPicture.php",
    // if you are using laravel ..., you dont need to put csrf in meta tag
    headers: {
        'X-CSRF-TOKEN': "{{ csrf_token() }}"
    },

});

myDropzone.options.profilePicture = {

    dictDefaultMessage: "This message not gonna work",

    paramName: "profile_picture",
};
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.