項目中常常會遇到文件上傳,管理多文件上傳過程的需求。
bootstrap file input組件算是不錯的解決方案javascript
項目Github地址:https://github.com/kartik-v/b...php
除此以外還有一個官方在線文檔,提供各類示例:http://plugins.krajee.com/fil...css
組件開發多年,功能強大,最簡單的集成方式卻並不複雜,首先下載源代碼:html
php composer.phar require kartik-v/bootstrap-fileinput "@dev"
插件兼容bootstrap3/4
引入相關文件:java
<!-- bootstrap 4.x is supported. You can also use the bootstrap css 3.3.x versions --> <link rel="stylesheet" href="/css/bootstrap.min.css"> <link href="/bootstrap-fileinput/4.4.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" /> <script src="/jquery-3.2.1.min.js"></script> <!-- the main fileinput plugin file --> <script src="/bootstrap-fileinput/4.4.9/js/fileinput.min.js"></script> <!-- optionally if you need translation for your language then include locale file as mentioned below --> <script src="/bootstrap-fileinput/4.4.9/js/locales/(lang).js"></script>
最簡單的初始化代碼以下:jquery
<input type="file" id="input-id" /> $("#input-id").fileinput();
但要完成一個基本可用的狀態,還須要增長如下配置項:git
$("#cover").fileinput({ language: "zh", showCaption: false, // 不顯示本地文件名 allowedFileTypes: ['image'], // 只容許上傳圖片 allowedFileExtensions: ["jpg", "jpeg", "png", "gif"], uploadUrl: "{{ url('uploads/image') }}" //上傳圖片的服務器地址 }).on('fileuploaded', function(event, data, previewId, index){ var response = data.response; $('input#coverUploader').attr('required', false); var input = $('<input type="hidden" name="cover" />'); input.attr('value', response.key); $('form').append(input); });
That's all.github