85

我正在使用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();

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

4

11 回答 11

196

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

<div class="dz-message" data-dz-message><span>Your Custom Message</span></div>
于 2014-04-10T15:27:30.757 回答
86

您可以使用以下命令更改所有默认消息

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.";
于 2016-08-03T00:33:54.263 回答
42

创建 dropzone 时,您可以像这样设置默认消息。

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'});
于 2015-01-13T15:27:29.507 回答
11

首先在表单中添加一个 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>
于 2015-09-11T11:24:43.570 回答
10

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

Dropzone.prototype.defaultOptions.dictDefaultMessage = "Your text";
于 2016-03-04T11:11:37.483 回答
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()
    {
     ....
于 2016-05-17T03:33:46.340 回答
6

我摆弄了几个小时。

出于某种原因,需要做以下三件事:

  1. 我的 dropzone 标签不能在我使用 dropzone 的同一页面上。我不得不在模板页面上引用它们
  2. 你要变成dropzone的元素必须有一个'dropzone'类
  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 选项就起作用了。

于 2020-04-22T18:30:43.420 回答
4

为了在 Asp.Net Razor Pages 中本地化 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

于 2019-03-11T05:42:54.123 回答
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.');
于 2014-05-14T15:25:10.110 回答
2

在 dropzone 的 css 中寻找

.dropzone .dz-default.dz-message

在这个类中删除

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

接下来要做的是搜索这个类

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

并将其更改为 display:block

于 2014-05-15T19:50:25.463 回答
1

如果您以编程方式创建 Dropzones,那么您必须设置您的选项,如下所示:

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",
};
于 2019-07-27T08:01:47.013 回答