javascript – 使用jQuery-File-Upload插件创建上传按钮和进度条
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用jQuery-File-Upload插件创建上传按钮和进度条,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4603字,纯文字阅读大概需要7分钟。
内容图文
我正在尝试使用jQuery File Upload插件以ajax形式上传一些文件.我正在尝试遵循“Basic” plugin的指示,但文档有点稀疏.
我没有看到任何方法来创建“开始上传”按钮.我也不太了解如何设置单个上传的进度条.我看到我可以在add回调中设置data.context,但是如果data.files中有多个文件,它是如何工作的?
解决方法:
所有标记必须包装在一个表单中,这里是表单的标记.
<form id="fileupload" action="/path/to/your/controller/ext" method="POST" type="multiplart/form-data">...everything below this goes in here </form>
这是创建“开始上传”按钮的标记.
<button class="btn btn-primary start" type="submit">
<span>Start upload</span>
</button>
这是创建“添加文件”按钮的标记.
<span class="btn btn-success fileinput-button">
<span>Add files...</span>
<input type="file" multiple="" name="files[]">
</span>
这是创建“取消上传”按钮的标记.
<button class="btn btn-warning cancel" type="reset">
<span>Cancel upload</span>
</button>
这是创建“删除”按钮的标记.
<button class="btn btn-danger delete" type="button">
<span>Delete</span>
</button>
这是显示单个文件进度的标记.每个文件都是同步处理的,这意味着此进度条将始终显示当前排队文件的进度.
<div class="span5 fileupload-progress fade">
<!-- The global progress bar -->
<div aria-valuemax="100" aria-valuemin="0" role="progressbar" class="progress progress-success progress-striped active">
<div style="width:0%;" class="bar"></div>
</div>
<!-- The extended global progress information -->
<div class="progress-extended"> </div>
</div>
这是用于在处理文件数据时保存文件数据的HTML.
<table class="table table-striped" role="presentation">
<tbody data-target="#modal-gallery" data-toggle="modal-gallery" class="files"></tbody>
</table>
您可以看到这是一个标准的“提交”按钮.它将用于处理我们的表单.单击它时,表单将尝试上载所有文件部分.
进度条的HTML,如上面的代码所假设的..
<div id="progress">
<div class="bar" style="width: 0%;"></div>
</div>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
}
});
});
根据网站,每个javascript文件都有关于它的功能和要求的评论.让我们分解吧
jQuery的
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
//We need the jQuery core. No need for an explanation.
jQuery UI Widget在整个过程中使用,如果**我们还没有包含jQuery UI核心或jQuery UI.widget核心,我们必须包含它们
<!-- The jQuery UI widget factory, can be omitted if jQuery UI is already included -->
<script src="js/vendor/jquery.ui.widget.js"></script>
有一个模板工厂插件用于在您将其拖放/添加到列表时自动生成元素.你想要包括这个.
<!-- The Templates plugin is included to render the upload/download listings -->
<script src="http://blueimp.github.com/JavaScript-Templates/tmpl.min.js"></script>
您想要能够调整大小和预览图像吗?我打赌你这样做
<!-- The Load Image plugin is included for the preview images and image resizing functionality -->
<script src="http://blueimp.github.com/JavaScript-Load-Image/load-image.min.js"></script>
这适用于HTML5 Canvas to Blob支持.它保持了上述的simliar功能,但是,HTML5上传是必需的.
<!-- The Canvas to Blob plugin is included for image resizing functionality -->
<script src="http://blueimp.github.com/JavaScript-Canvas-to-Blob/canvas-to-blob.min.js"></script>
下一个是非常不言自明的,我们不需要这些,但他将它们用于演示.
<!-- Bootstrap JS and Bootstrap Image Gallery are not required, but included for the demo -->
<script src="http://blueimp.github.com/cdn/js/bootstrap.min.js"></script>
<script src="http://blueimp.github.com/Bootstrap-Image-Gallery/js/bootstrap-image-gallery.min.js"></script>
如果浏览器不支持XHR文件上传,那么我们在后台使用iFrame来模仿功能.您需要这个以获得浏览器支持.
<!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
<script src="js/jquery.iframe-transport.js"></script>
其余的是与Plugin相关的核心文件.
<!-- The basic File Upload plugin -->
<script src="js/jquery.fileupload.js"></script>
<!-- The File Upload file processing plugin -->
<script src="js/jquery.fileupload-fp.js"></script>
<!-- The File Upload user interface plugin -->
<script src="js/jquery.fileupload-ui.js"></script>
下一个不太自我夸大.本地化处理语言差异化.
<!-- The localization script -->
<script src="js/locale.js"></script>
最后,这是我们土豆的肉. Main.js处理我们所有的脚本执行和条件化.这是您想要熟悉的文件.如果你检查他们的页面,你会看到正在发生的一切.一个简单的复制粘贴就足够了;但是,您需要更改此脚本中的URL值以匹配您计划使用的服务器.
<!-- The main application script -->
<script src="js/main.js"></script>
祝好运.
内容总结
以上是互联网集市为您收集整理的javascript – 使用jQuery-File-Upload插件创建上传按钮和进度条全部内容,希望文章能够帮你解决javascript – 使用jQuery-File-Upload插件创建上传按钮和进度条所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。