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

时间:2013-07-17 14:23:07

标签: javascript jquery

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

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

11 个答案:

答案 0 :(得分:133)

在dropzone表单中添加元素,如下所示:

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

答案 1 :(得分:43)

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

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.";

答案 2 :(得分:30)

创建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'});

答案 3 :(得分:7)

首先在表单中添加一个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>

答案 4 :(得分:7)

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

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

答案 5 :(得分:4)

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()
    {
     ....

答案 6 :(得分:4)

我弄了几个小时。

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

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

答案 7 :(得分:1)

如果您对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.');

答案 8 :(得分:1)

在dropzone的css中寻找

.dropzone .dz-default.dz-message

在此课程中删除

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

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

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

并将其更改为display:block

答案 9 :(得分:1)

为了在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

答案 10 :(得分:0)

如果要以编程方式创建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",
};