如何使用HTML5,JavaScript和Bootstrap构建自定义文件上传器

by Prashant Yadav

通过Prashant Yadav

如何使用HTML5,JavaScript和Bootstrap构建自定义文件上传器 (How to build a custom file uploader with HTML5, JavaScript, & Bootstrap)

In this short article, we’ll learn how to create custom file uploader with JQuery, ES6, and Bootstrap4.

在这篇简短的文章中,我们将学习如何使用JQuery, ES6和Bootstrap4创建自定义文件上传器。

We will create a file uploader with a custom design and an option to preview selected files and remove them.

我们将创建一个具有自定义设计的文件上传器,并提供预览所选文件并将其删除的选项。

Support me by reading this article here.

通过在这里阅读本文来支持我。

演示版 (Demo)

Check out the live demo here.

在此处查看现场演示。

实作 (Implementation)

  • We will use the html5 file uploader to upload the files.

    我们将使用html5文件上传器上传文件。
  • Then, with the help of Bootstrap popover, we will preview the selected files.

    然后,借助Bootstrap弹出窗口,我们将预览选定的文件。
  • While previewing the files, we will provide an option to remove the selected file.

    在预览文件时,我们将提供一个选项来删除选定的文件。
  • As JQuery is one of the dependencies for Bootstrap popover, we will use it to ease our work.

    由于JQuery是Bootstrap popover的依赖项之一,因此我们将使用它来简化我们的工作。

依存关系 (Dependencies)

文件上传器HTML布局 (HTML layout for file uploader)

Explanation

说明

  • We have created a container named custom-file-picker.

    我们创建了一个名为custom-file-picker的容器。

  • In this, we have our custom file upload picture-container and our popover previewer popover-container.

    在这里,我们有自定义文件上传picture-container和我们的popover预览器popover-container

  • Every file picker has a unique id a8755cf0-f4d1-6376-ee21-a6defd1e7c08 and its corresponding popover refers to that id data-target="a8755cf0-f4d1-6376-ee21-a6defd1e7c08" to preview the files.

    每个文件选择器都有一个唯一的id a8755cf0-f4d1-6376-ee21-a6defd1e7c08 ,其对应的弹出窗口引用id data-target="a8755cf0-f4d1-6376-ee21-a6defd1e7c08"来预览文件。

造型我们的组件 (Styling our components)

处理功能 (Handling the functionality)

Now that we have styled our components, it is time to handle the functionality. We will use Jquery with ES6 to make things easy.

既然我们已经对组件进行了样式设置,那么现在该处理功能了。 我们将在ES6中使用Jquery来使事情变得容易。

储存档案 (Storing the files)

We will create a global variable to store the files.

我们将创建一个全局变量来存储文件。

We will use this variable to store all the files of the corresponding file picker with the help of its id.

我们将使用此变量在其ID的帮助下存储相应文件选择器的所有文件。

Now we will create a function which will manage the storing of the file and displaying the count of the files. This function will take id and array of files as input.

现在,我们将创建一个函数,该函数将管理文件的存储并显示文件的数量。 此函数将idarray of files作为输入。

$(`[data-id="${id}"] > .file-total-viewer`).text(files.length); will update the file count in popover previewer.

$(`[data-id="${id}"] > .file-total-viewer`).text(files.lengt h); 将在弹出预览器中更新文件计数。

处理文件选取 (Handling the file picking)

We have our function ready to update the count and store the files. We will just pass data to this function once the files are selected or changed.

我们已经准备好更新计数和存储文件的功能。 一旦选择或更改了文件,我们只会将数据传递给此功能。

Once the files are selected we will show the complete animation with SVG to notify users that files are changed.

选择文件后,我们将显示带有SVG的完整动画,以通知用户文件已更改。

Right now we have our file stored and the count visible. Let's create the file previewer with a bootstrap popover.

现在,我们已经存储了文件,并且计数可见。 让我们用引导弹出窗口创建文件预览器。

Bootstrap provides us a method to dynamically generate the content of the popover. So we attach the popover to [data-toggle="popover"]. Learn more about it here.

Bootstrap为我们提供了一种动态生成弹出窗口内容的方法。 因此,我们将弹出窗口附加到[data-toggle="popover"] 。 在此处了解更多信息。

这个怎么运作 (How it works)

  • Every time a popup is about to render it will use its [data-target] id and pull all the files from the fileStorage.

    每当弹出窗口将要呈现时,它将使用其[data-target] id并从fileStorage拉出所有文件。

  • If there are files, then render those files along with the delete button.

    如果有文件,则渲染这些文件以及“删除”按钮。
  • If there is no file, then show some message.

    如果没有文件,则显示一些消息。

Now in case you have multiple file uploaders and you want only one popover to be open at a time, add the following code.

现在,如果您有多个文件上传器,并且一次只希望打开一个弹出窗口,请添加以下代码。

If you select some file and click on view you should be able to view it. Now the last thing we will do is handle the deletion of files.

如果选择某些文件并单击view ,则应该可以查看它。 现在,我们要做的最后一件事是处理文件的删除。

删除文件 (Deleting the file)

We have provided the id of the file picker to the delete button through data-target and the name of the file through data-name. Every time the delete icon is clicked we will use these values to remove the files.

我们通过data-target为删除按钮提供了文件选择器的ID,并通过data-target name为文件名称提供了文件data-name 。 每次单击删除图标时,我们将使用这些值来删除文件。

As we are dynamically generating the content of the popover and it does not already exist in the DOM, we cannot assign an event to it. So we have to use a workaround of assigning an event on the DOM and checking if the delete icon is clicked with $(document).on('click', '.popover-content-remove', function (e) {});.

由于我们正在动态生成弹出窗口的内容并且它在DOM中尚不存在,因此我们无法为其分配事件。 因此,我们必须使用一种变通方法,在DOM上分配一个事件,并检查是否使用$(document).on('click', '.popover-content-remove', function (e) {});单击删除图标。 $(document).on('click', '.popover-content-remove', function (e) {});

这个怎么运作 (How it works)

  • Once the delete icon is clicked we will ask for confirmation from the user.

    单击删除图标后,我们将要求用户确认。
  • If the user wants to proceed, then we fetch the id and the name assigned to delete button through data-target and data-name.

    如果用户想要继续,那么我们通过data-targetdata-name获取ID和分配给delete按钮data-name

  • We remove that particular file using filter() method.

    我们使用filter()方法删除该特定文件。
  • Once the file is removed from the array, then we update its count by passing the value to our helper function storeFile(id, newArr);.

    一旦文件从数组中删除,我们就通过将值传递给我们的辅助函数storeFile(id, newArr);来更新其计数storeFile(id, newArr);

  • Also, we remove the element from the popover. If the array is empty then show some message.

    另外,我们从弹出窗口中删除该元素。 如果数组为空,则显示一些消息。

Note: You should provide a unique id to each file picker and its popover previewer.

注意:您应该为每个文件选择器及其弹出预览器提供唯一的ID。

完整的代码 (Complete code)

If you liked this article, please give it 50+ ?and share it! If you have any questions related to this feel free to ask me.

如果您喜欢这篇文章,请给它50+并分享! 如果您对此有任何疑问,请随时问我。

For more like this and algorithmic solutions in Javascript, follow me on Twitter. I write about ES6, React, Nodejs, Data structures, and Algorithms on learnersbucket.com.

有关Java的更多此类信息和算法解决方案,请在 Twitter上 关注我 我写ES6 ,React过来,的NodeJS, 数据结构和算法上learnersbucket.com

翻译自: https://www.freecodecamp.org/news/custom-file-uploader-with-html5-javascript-bootstrap-85a56a0437c5/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392097.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

monkey测试===通过monkey测试检查app内存泄漏和cpu占用

最近一直在研究monkey测试。网上资料很多,但都是一个抄一个的。原创的很少 我把检查app内存泄漏的情况梳理一下: 参考资料: Monkey测试策略:https://testerhome.com/topics/597 Android Monkey测试详细介绍:http://www…

数据挖掘—主成分分析法降维和最小最大规范化

算法步骤:1)将原始数据按列组成n行m列矩阵X2)特征中心化。即每一维的数据都减去该维的均值,使每一维的均值都为03)求出协方差矩阵4)求出协方差矩阵的特征值及对应的特征向量5)将特征向量按对应的特征值大小从上往下按行排列成矩阵,取前k行组成矩阵p6)YPX…

用户使用说明c语言,(C语言使用指南.docx

(C语言使用指南Turbo C(V2.0)使用指南(本文的许多命令或方法同样适用于TC3) 在开始看本文以前,我先说明一下C语言的安装和使用中最应该注意的地方:许多网友在下载Turbo C 2.0和Turbo C 3.0后,向我问得最多的是在使用过程中碰到如下问题&…

三维空间两直线/线段最短距离、线段计算算法 【转】

https://segmentfault.com/a/1190000006111226d(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−bd(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−b2|具体实现代码如下(C#实现): public bool IsEqual(double d1, double d2) { …

【慎思堂】之JS牛腩总结

一 JS基础 1-定义 Javascript是一种脚本语言/描述语言,是一种解释性语言。用于开发交互式web网页,使得网页和用户之间实现了一种实时性的、动态的、交互性的关系,使网页包含更多活跃的元素和更加精彩的内容。 主要用于:表单验证 …

vuejs 轮播_如何在VueJS中设计和构建轮播功能

vuejs 轮播by Fabian Hinsenkamp由Fabian Hinsenkamp设计 A carousel, slideshow, or slider — however you call it this class of UI — has become one of the core elements used in modern web development. Today, it’s almost impossible to find any Website or UI …

iOS绘圆形图-CGContextAddArc各参数说明

2019独角兽企业重金招聘Python工程师标准>>> 1.使用 UIGraphicsGetCurrentContext() 画圆 CGContextAddArc(<#CGContextRef _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFlo…

c语言中if和goto的用法,C语言中if和goto的用法.doc

C语言中if和goto的用法C语言中&#xff0c;if是一个条件语句&#xff0c;用法??if(条件表达式) 语句如果满足括号里面表达式&#xff0c;表示逻辑为真于是执行后面的语句&#xff0c;否则不执行(表达式为真则此表达式的值不为0&#xff0c;为假则为0&#xff0c;也就是说&…

数据挖掘—K-Means算法(Java实现)

算法描述 &#xff08;1&#xff09;任意选择k个数据对象作为初始聚类中心 &#xff08;2&#xff09;根据簇中对象的平均值&#xff0c;将每个对象赋给最类似的簇 &#xff08;3&#xff09;更新簇的平均值&#xff0c;即计算每个对象簇中对象的平均值 &#xff08;4&#xf…

自我价值感缺失的表现_不同类型的缺失价值观和应对方法

自我价值感缺失的表现Before handling the missing values, we must know what all possible types of it exists in the data science world. Basically there are 3 types to be found everywhere on the web, but in some of the core research papers there is one more ty…

[收藏转载]C# GDI+ 简单绘图(一)

最近对GDI这个东西接触的比较多&#xff0c;也做了些简单的实例&#xff0c;比如绘图板&#xff0c;仿QQ截图等&#xff0e; 废话不多说了&#xff0c;我们先来认识一下这个GDI&#xff0c;看看它到底长什么样. GDI&#xff1a;Graphics Device Interface Plus也就是图形设备接…

mybaties总结+hibernate总结

一、对原生态jdbc程序中问题总结 1.1 jdbc程序 需求&#xff1a;使用jdbc查询mysql数据库中用户表的记录 statement:向数据库中发送一个sql语句 预编译statement&#xff1a;好处&#xff1a;提高数据库性能。 预编译statement向数据库中发送一个sql语句&#xff0c;数据库编译…

客户旅程_我如何充分利用freeCodeCamp的旅程

客户旅程by Catherine Vassant (aka Codingk8)由凯瑟琳瓦森(Catherine Vassant)(又名Codingk8) 我如何充分利用freeCodeCamp的旅程 (How I made the most out of my freeCodeCamp journey) 我的路线图&#xff1f; ️超越课程范围的reeCodeCamp (My road map ?️ to freeCode…

Python14 函数

函数 面向对象编程&#xff1a; 类----class 面向过程编程&#xff1a;过程---def 函数式编程&#xff1a;函数---def def test(x):描述x 1return x#def是定义函数的关键字#test是函数名称#&#xff08;x&#xff09;是参数#x1是 函数体&#xff0c;是一段逻辑代码#return 定义…

学习sql注入:猜测数据库_面向数据科学家SQL:学习简单方法

学习sql注入:猜测数据库We don’t pick a hammer and look for nails — that would be an unusual way of solving problems. The usual way of doing business is to identify the problem first, then look for appropriate tools.我们不用锤子找钉子&#xff0c;那是解决问…

android 百度地图3.0,android 百度地图3.0

一&#xff1a;为地图设置事件注意新版本中要有一个getMapmMapView.getMap().setOnMapStatusChangeListener(listener);OnMapStatusChangeListener listener newOnMapStatusChangeListener() {/*** 手势操作地图&#xff0c;设置地图状态等操作导致地图状态开始改变。* param s…

(摘录)sockaddr与sockaddr_in,sockaddr_un结构体详细讲解

struct sockaddr { unsigned short sa_family; /* address family, AF_xxx */ char sa_data[14]; /* 14 bytes of protocol address */ }; sa_family是地址家族&#xff0c;一般都是“AF_xxx”的形式。好像通常大多用的是都是AF_INET。 sa_data是14字节协议…

数据挖掘—K-中心点聚类算法(Java实现)

K-中心点聚类算法 &#xff08;1&#xff09;任意选择k个对象作为初始的簇中心点 &#xff08;2&#xff09;指派每个剩余对象给离他最近的中心点所表示的簇 &#xff08;3&#xff09;选择一个未被选择的中心点直到所有的中心点都被选择过 &#xff08;4&#xff09;选择一个…

使用akka构建高并发程序_如何使用Akka Cluster创建简单的应用程序

使用akka构建高并发程序If you read my previous story about Scalachain, you probably noticed that it is far from being a distributed system. It lacks all the features to properly work with other nodes. Add to it that a blockchain composed by a single node is…

pandas之数值计算与统计

数值计算与统计 对于DataFrame来说&#xff0c;求和、最大、最小、平均等统计方法&#xff0c;默认是按列进行统计&#xff0c;即axis 0&#xff0c;如果添加参数axis 1则会按照行进行统计。 如果存在空值&#xff0c;在统计时默认会忽略空值&#xff0c;如果添加参数skipna …