本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下
1.簡介:WebUploader是由Baidu WebFE(FEX)團隊開發的一個簡單的以HTML5為主,FLASH為輔的現代文件上傳組件。在現代的瀏覽器里面能充分發揮HTML5的優勢,同時又不摒棄主流IE瀏覽器,沿用原來的FLASH運行時,兼容IE6+,iOS 6+, android 4+。兩套運行時,同樣的調用方式,可供用戶任意選用。
2.引入資源:使用Web Uploader文件上傳需要引入三種資源:JS, CSS, SWF。
1
2
3
4
5
6
|
<!--引入CSS--> < link rel = "stylesheet" type = "text/css" href = "webuploader文件夾/webuploader.css" > <!--引入JS--> < script type = "text/javascript" src = "webuploader文件夾/webuploader.js" ></ script > <!--SWF在初始化的時候指定,在后面將展示--> |
3.HTML部分
1
2
3
4
5
6
7
8
9
|
< div id = "uploader" class = "wu-example" > <!--用來存放文件信息--> < ul id = "thelist" class = "list-group" ></ ul > < div class = "uploader-list" ></ div > < div class = "btns" > < div id = "picker" style = "float:left;" >選擇文件</ div > < input id = "ctlBtn" type = "button" value = "開始上傳" class = "btn btn-default" style = "width:78px;height:37px;margin-left:10px;" /> </ div > </ div > |
4.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
//初始化上傳控件 function initUpload() { var $ = jQuery; var $list = $( '#thelist' ); var uploader = WebUploader.create({ // 選完文件后,是否自動上傳。 auto: false , // swf文件路徑 swf: applicationPath + '../Content/scripts/plugins/webuploader/Uploader.swf' , // 文件接收服務端。 server: applicationPath + 'PublicInfoManage/Upload/Upload' , // 選擇文件的按鈕??蛇x。 // 內部根據當前運行是創建,可能是input元素,也可能是flash. pick: '#picker' , chunked: true , //開始分片上傳 chunkSize: 2048000, //每一片的大小 formData: { guid: GUID //自定義參數,待會兒解釋 }, // 不壓縮image, 默認如果是jpeg,文件上傳前會壓縮一把再上傳! resize: false }); // 當有文件被添加進隊列的時候 uploader.on( 'fileQueued' , function (file) { $list.append( '<li id="' + file.id + '" class="list-group-item">' + '<span class="fileName" dataValue="">' + file.name + '</span>' + '<span class="state" style=\" margin-left: 10px;\">等待上傳</span>' + '<span class="filepath" dataValue="0" style=\" margin-left: 10px;display: none;\"></span>' + '<span class="download" style="margin-left:20px;"></span>' + '<span class="webuploadDelbtn"style=\"float: right;display: none; \">刪除<span>' + '</li>' ); }); // 文件上傳過程中創建進度條實時顯示。 uploader.on( 'uploadProgress' , function (file, percentage) { var $li = $( '#' + file.id), $percent = $li.find( '.progress .progress-bar' ); // 避免重復創建 if (!$percent.length) { $percent = $( '<div class="progress progress-striped active">' + '<div class="progress-bar" role="progressbar" style="width: 0%">' + '</div>' + '</div>' ).appendTo($li).find( '.progress-bar' ); } $li.find( 'span.state' ).text( '上傳中' ); $percent.css( 'width' , percentage * 100 + '%' ); }); // 文件上傳成功,給item添加成功class, 用樣式標記上傳成功。 uploader.on( 'uploadSuccess' , function (file, response) { var $li = $( '#' + file.id); //$('#' + file.id).find('p.state').text('已上傳'); $.post( '../../PublicInfoManage/Upload/Merge' , { guid: GUID, fileName: file.name }, function (data) { $li.find( 'span.state' ).html("上傳成功 "); $li.find('span.filepath').attr(" dataValue ", 1); $li.find('span.fileName').attr(" dataValue ", data.filename); $li.find('span.fileName').html(data.filename); $li.find('span.download').html(" <a href=\ "../../PublicInfoManage/Upload/DownFile?filePath=" + data.filepath + "&fileName=" + data.filename + "\">下載</a>" ) $li.find( 'span.webuploadDelbtn' ).show(); $li.find( 'span.filepath' ).html(data.filepath); //增加列表存儲 files.push(data); }); }); // 文件上傳失敗,顯示上傳出錯。 uploader.on( 'uploadError' , function (file, reason) { $( '#' + file.id).find( 'p.state' ).text(reason); }); // 完成上傳完了,成功或者失敗,先刪除進度條。 uploader.on( 'uploadComplete' , function (file) { $( '#' + file.id).find( '.progress' ).fadeOut(); }); //所有文件上傳完畢 uploader.on( "uploadFinished" , function () { //提交表單 }); //開始上傳 $( "#ctlBtn" ).click( function () { uploader.upload(); }); //刪除 $list.on( "click" , ".webuploadDelbtn" , function () { debugger var $ele = $( this ); var id = $ele.parent().attr( "id" ); var file = uploader.getFile(id); uploader.removeFile(file); $ele.parent().remove(); //移除數組 var destFile = findFile(file.name) var index = files.indexOf(destFile); if (index > -1) { files.splice(index, 1); } }); } |
5.C# Controller后臺處理
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
|
/// <summary> /// 上傳文件 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Upload() { string fileName = Request[ "name" ]; int lastIndex = fileName.LastIndexOf( '.' ); string fileRelName = lastIndex == -1? fileName: fileName.Substring(0, fileName.LastIndexOf( '.' )); fileRelName = fileRelName.Replace( "[" , "" ).Replace( "]" , "" ).Replace( "{" , "" ).Replace( "}" , "" ).Replace( "," , "" ); int index = Convert.ToInt32(Request[ "chunk" ]); //當前分塊序號 var guid = Request[ "guid" ]; //前端傳來的GUID號 var dir = Server.MapPath( "~/Upload/file" ); //文件上傳目錄 string currentTime = DateTime.Now.ToString( "yyyy-MM-dd" ); dir += "\\" + currentTime; dir = Path.Combine(dir, fileRelName); //臨時保存分塊的目錄 if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir); string filePath = Path.Combine(dir, index.ToString()); //分塊文件名為索引名,更嚴謹一些可以加上是否存在的判斷,防止多線程時并發沖突 var data = Request.Files[ "file" ]; //表單中取得分塊文件 //if (data != null)//為null可能是暫停的那一瞬間 //{ data.SaveAs(filePath); //報錯 //} return Json( new { erron = 0 }); //Demo,隨便返回了個值,請勿參考 } |
6.實現效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Gary_888/article/details/79545182