前言:
多圖上傳在一些特殊的需求中我們經常會遇到,其實多圖上傳的原理大家都有各自的見解。對于Layui多圖上傳和我之前所說的通過js獲取文本框中的文件數組遍歷提交的原理一樣,只不過是Layui中的upload.render方法已經幫我們封裝好了,我們只管調用即可,也就是說你選中了幾張圖片,那么將會向后臺請求與圖片張數相同的次數,即為遍歷提交的方式。
Layui文件/圖片樣式地址(官方文檔):https://www.layui.com/demo/upload.html
一、引入Layui.cs和Layui.js:
需要本地項目中存在layui相關樣式和js,非網絡地址
<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css" rel="external nofollow" media="all">
<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
當然假如你需要有彈框提示的話,你還需要引入Layer.js
二、前端代碼:
a.Html中的代碼:
1
2
3
4
5
6
7
8
9
10
11
|
< li > < h6 >滾動圖片:</ h6 > < div class = "layui-upload" > < button type = "button" class = "layui-btn" id = "test2" >滾動圖片上傳【推薦上傳三張】</ button > < button type = "button" class = "layui-btn layui-btn-danger" onclick = "clearAll()" >< i class = "layui-icon" ></ i ></ button > < input type = "hidden" name = "ScrollingGraph" id = "ScrollingGraph" /> < blockquote class = "layui-elem-quote layui-quote-nm" style = "margin-top: 10px;" > 預覽圖: < div class = "layui-upload-list" id = "demo2" ></ div > </ blockquote > </ div > </ li > |
b.js中的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<script> layui.use( 'upload' , function () { var $ = layui.jquery, upload = layui.upload; //多圖片上傳 upload.render({ elem: '#test2' , url: '/FileUpload/FileLoad/' , multiple: true , //允許選擇多張圖片 before: function (obj) { //預讀本地文件示例,不支持ie8 obj.preview( function (index, file, result) { $( '#demo2' ).append( '<img src="' + result + '" id="codetool">
三、服務端接口圖片文件流,并保存:
四、效果圖展示:
總結 以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。 原文鏈接:https://www.cnblogs.com/Can-daydayup/p/11125324.html 延伸 · 閱讀
精彩推薦
|