温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - Filepond customize droparea
css filepond html javascript

javascript - Filepond自定义拖放区

发布于 2020-11-11 16:04:58

使用filepond时,我需要自定义filepond拖放区,这意味着我需要添加一些带有一些占位符图像的自定义HTML,以使用户了解应上传哪种图像并应允许多次上传。

在此处输入图片说明

有没有办法做到这一点?

我想用占位符添加一个绝对定位的着色器,但在上传自定义元素时无法覆盖。

这是我在Filepond内部使用react的方式:

...
    return (
      <div className="uploads">
        <div className="uploads__placeholders">{placeholderImages}</div>
        <FilePond
          ref={(ref) => (this.pond = ref)}
          files={this.state.files}
          labelIdle={""}
          allowMultiple={true}
          maxTotalFileSize={10485760}
          acceptedFileTypes={this.props.acceptedFileTypes}
          labelMaxTotalFileSize={"Total file size should be lesser than 10MB."}
          maxFiles={maxFilesAllowed}
          allowProcess={this.props.process ? this.props.process : true}
          imagePreviewHeight={135}
          //imagePreviewTransparencyIndicator={"grid"}
          server={{...}}
          onremovefile={(file) => {...}}
          oninit={(t) => {...}}
          onupdatefiles={(fileItems) => {...}}
        />
      </div>
    );
...

因此,我创建了一个自定义包装器,并在filepond包装器的顶部与css对齐,但这似乎不是理想的解决方法。

查看更多

提问者
fefe
被浏览
5
Islam Elshobokshy 2020-06-19 21:49

关键的答案是使用labelIdlewithbeforeAddFile选项,它为您提供了一种方法来更改默认模板HTML,并预先删除您想要的任何内容。您应该将其添加到Filepond初始化中。

我注意确定如何在react中删除内容,但是jQuery示例将是这样的:

FilePond.parse(document.body);
const inputElement = document.querySelector('#file_upload');

FilePond.registerPlugin(FilePondPluginImagePreview);

const pond = FilePond.create(inputElement, {
  allowMultiple: true,
  imagePreviewHeight: 135,
  labelIdle: `
    <div style="width:100%;height:100%;">
    	<p>
        Drag &amp; Drop your files or <span class="filepond--label-action" tabindex="0">Browse</span><br>
        Some samples to give you an idea :
      </p>
    </div>
    <div class="images" id="allImages">
       <div class="images_child">
          <img src="https://live.staticflickr.com/4561/38054606355_26429c884f_b.jpg">
       </div>
       <div class="images_child">
          <img src="https://live.staticflickr.com/4561/38054606355_26429c884f_b.jpg">
       </div>
       <div class="images_child">
          <img src="https://live.staticflickr.com/4561/38054606355_26429c884f_b.jpg">
       </div>
     </div>
  `,
  beforeAddFile (e) {
  	$('#allImages').html('');
  }
});
.filepond--drop-label {
  background-color: #ECF7E9;
  height: auto!important;
}

.images {
  display: inline-block;
  padding: 0 5px;
}

.images_child {
  display: contents;
  padding: 0 5px;
  text-align: center;
}

.filepond--root * {
  height: auto;
  padding: 0 4px;
}

img {
  width: 25%;
  opacity: 0.8;
  filter: grayscale(100%);
  border-radius: 8px;
  cursor: pointer;
}


/* Responsive layout - makes a two column-layout instead of four columns */

@media screen and (max-width: 800px) {
  .column {
    flex: 50%;
    max-width: 50%;
  }
}


/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .column {
    flex: 100%;
    max-width: 100%;
  }
}
<script src="https://unpkg.com/filepond-plugin-image-preview@4.6.4/dist/filepond-plugin-image-preview.js"></script>
<script src="https://unpkg.com/filepond@4.17.1/dist/filepond.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="https://unpkg.com/filepond@4.17.1/dist/filepond.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://unpkg.com/filepond-plugin-image-preview@4.6.4/dist/filepond-plugin-image-preview.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>

<input type="file" name='file' class='filepond' multiple id='file_upload' />

https://jsfiddle.net/d6e2nh93/

免责声明:

1)我没有做完整的CSS作为您的图像示例,因为我认为没有必要,您可以自己轻松完成。

2)这不是一个 React 性的答案,而是一个指导您如何正确使用的答案,filepond以实现所需的结果。