首頁  >  文章  >  web前端  >  教你使用bootstrap file input上傳圖片文件

教你使用bootstrap file input上傳圖片文件

不言
不言轉載
2018-10-13 15:39:534482瀏覽

這篇文章帶給大家的內容是關於教你使用bootstrap file input上傳圖片文件,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

專案中常會遇到檔案上傳,管理多檔案上傳過程的需求。

bootstrap file input元件算是不錯的解決方案

專案Github位址:https://github.com/kartik-v/b...

##元件開發多年,功能強大,最簡單的整合方式卻不複雜,首先下載原始碼:

php composer.phar require kartik-v/bootstrap-fileinput "@dev"
外掛程式相容bootstrap3/4

引入相關檔案:

<!-- 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>
最簡單的初始化程式碼如下:

<input type="file" id="input-id" />
$("#input-id").fileinput();
但要完成一個基本上可用的狀態,還需要增加以下配置項:

$("#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);
});
以上就是本篇文章的全部內容了,更多bootstrap的內容,大家可以關注php中文網的bootstrap教程

#

以上是教你使用bootstrap file input上傳圖片文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除