1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php dropzone.js中文教程 使用Dropzone.js上传的示例代码

php dropzone.js中文教程 使用Dropzone.js上传的示例代码

时间:2023-07-16 23:29:51

相关推荐

php dropzone.js中文教程 使用Dropzone.js上传的示例代码

本文介绍了使用Dropzone.js上传的示例代码,分享给大家,具体如下:

说明:后台用的python的flask框架,后台对你理解这篇文章没什么影响,你可以使用php

form作为上传区

引入Dropzone.js和dropzone.css然后使用表单form定义一个class=”dropzone”即可完成

Flask upload with Dropzone example

效果

div作为上传区

div作为上传区也很简单

Flask upload with Dropzone example点我上传

//下面两行是js和jquery的方式实现绑定div的例子,你选择一种即可

//var myDropzone = new Dropzone("#myId", { url: "{{ url_for('upload_file') }}" });

$("#myId").dropzone({ url: "{{ url_for('upload_file') }}" });

效果

form作为上传区配置

配置也分为两种,如果使用的form表单上传的就用如下方式配置

Flask upload with Dropzone example

//两种配置方式,第一种,表单上传时的配置方式,可以打开form表单的注释,myAwesomeDropzone是表单的id

Dropzone.options.myAwesomeDropzone = {

paramName: "file", // The name that will be used to transfer the file

maxFilesize: 2, // MB

accept: function(file, done) {

if (file.name != "justinbieber.jpg") {

done("Naha, you don't.");

}else {

done();

}

}

};

效果

div作为上传区配置

Flask upload with Dropzone example点我上传

//第二种配置,这种使用的是div做上传区域时使用的配置

Dropzone.autoDiscover = false;//不知道该行有什么用,欢迎高手下方评论解答

$("#myId").dropzone({

url: "{{ url_for('upload_file') }}",

addRemoveLinks: true,

method: 'post',

filesizeBase: 1024

});

说明:关于其他的配置请看最后的链接

主题

第一种

Dropzone.autoDiscover = false;

html, body {

height: 100%;

}

#actions {

margin: 2em 0;

}

/* Mimic table appearance */

div.table {

display: table;

}

div.table .file-row {

display: table-row;

}

div.table .file-row > div {

display: table-cell;

vertical-align: top;

border-top: 1px solid #ddd;

padding: 8px;

}

div.table .file-row:nth-child(odd) {

background: #f9f9f9;

}

/* The total progress gets shown by event listeners */

#total-progress {

opacity: 0;

transition: opacity 0.3s linear;

}

/* Hide the progress bar when finished */

#previews .file-row.dz-success .progress {

opacity: 0;

transition: opacity 0.3s linear;

}

/* Hide the delete button initially */

#previews .file-row .delete {

display: none;

}

/* Hide the start and cancel buttons and show the delete button */

#previews .file-row.dz-success .start,

#previews .file-row.dz-success .cancel {

display: none;

}

#previews .file-row.dz-success .delete {

display: block;

}

Configuration Demo

Add files...

Start upload

Cancel upload

Start

Cancel

Delete

// Get the template HTML and remove it from the doument

var previewNode = document.querySelector("#template");

previewNode.id = "";

var previewTemplate = previewNode.parentNode.innerHTML;

//开始先删除单个文件的布局

previewNode.parentNode.removeChild(previewNode);

var myDropzone = new Dropzone(document.body, { // 指定拖拽区为body

url: "{{ url_for('upload_file') }}", // Set the url

thumbnailWidth: 80,

thumbnailHeight: 80,

parallelUploads: 20,

previewTemplate: previewTemplate,

autoQueue: false, // 当队列有文件,是否立刻自动上传到服务器

previewsContainer: "#previews", // 指定存放文件队列区

clickable: ".fileinput-button" // 点击某个按钮或区域后出现选择电脑中本地图片,默认是previewsContainer指定的区域

});

myDropzone.on("addedfile", function(file) {

// 让模版中的单个文件可以点击上传

file.previewElement.querySelector(".start").onclick = function() { myDropzone.enqueueFile(file); };

});

// 显示所有文件整体上传进度1-100

myDropzone.on("totaluploadprogress", function(progress) {

document.querySelector("#total-progress .progress-bar").style.width = progress + "%";

});

myDropzone.on("sending", function(file) {

// 显示整体的上传的进度条,说明:原来是0,所以上面的style.width = progress + "%"即使是100%也看不到

document.querySelector("#total-progress").style.opacity = "1";

// 失效上传按钮

file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");

});

// 当没有文件上传时,隐藏进度条

myDropzone.on("queuecomplete", function(progress) {

document.querySelector("#total-progress").style.opacity = "0";

});

// 上传所有

document.querySelector("#actions .start").onclick = function() {

myDropzone.enqueueFiles(myDropzone.getAcceptedFiles());

//myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));与上面一样,可查看源码对比

};

//取消所有

document.querySelector("#actions .cancel").onclick = function() {

myDropzone.removeAllFiles(true);

};

第二种效果与默认的一样

Flask upload with Dropzone example✔✘

Dropzone.autoDiscover = false;//解决两次实例Dropzone错误,可在控制台看到该错误

$("#myId").dropzone({

url: "{{ url_for('upload_file') }}",

addRemoveLinks: true,

method: 'post',

filesizeBase: 1024,

previewTemplate: $('#preview-template').html(),//如果去掉该选项就会使用默认的

autoQueue: true,

init: function() {

this.on("addedfile", function(file) {

$(".start").click (function() {

this.enqueueFile(file);

})

});

}

});

demo文件

如果是flask框架可进行测试点击此处下载,如果是php或者其他就看看不必下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。