ASP.NET MVC Webuploader實現(xiàn)上傳功能
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下
1.簡介:WebUploader是由Baidu WebFE(FEX)團隊開發(fā)的一個簡單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。在現(xiàn)代的瀏覽器里面能充分發(fā)揮HTML5的優(yōu)勢,同時又不摒棄主流IE瀏覽器,沿用原來的FLASH運行時,兼容IE6+,iOS 6+, android 4+。兩套運行時,同樣的調用方式,可供用戶任意選用。
2.引入資源:使用Web Uploader文件上傳需要引入三種資源:JS, CSS, SWF。
<!--引入CSS--> <link rel="stylesheet" type="text/css" href="webuploader文件夾/webuploader.css"> <!--引入JS--> <script type="text/javascript" src="webuploader文件夾/webuploader.js"></script> <!--SWF在初始化的時候指定,在后面將展示-->
3.HTML部分
<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部分
//初始化上傳控件
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',
// 選擇文件的按鈕。可選。
// 內部根據當前運行是創(chuàng)建,可能是input元素,也可能是flash.
pick: '#picker',
chunked: true,//開始分片上傳
chunkSize: 2048000,//每一片的大小
formData: {
guid: GUID //自定義參數(shù),待會兒解釋
},
// 不壓縮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>');
});
// 文件上傳過程中創(chuàng)建進度條實時顯示。
uploader.on('uploadProgress', function (file, percentage) {
var $li = $('#' + file.id),
$percent = $li.find('.progress .progress-bar');
// 避免重復創(chuàng)建
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();
//移除數(shù)組
var destFile = findFile(file.name)
var index = files.indexOf(destFile);
if (index > -1) {
files.splice(index, 1);
}
});
}
5.C# Controller后臺處理
/// <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());//分塊文件名為索引名,更嚴謹一些可以加上是否存在的判斷,防止多線程時并發(fā)沖突
var data = Request.Files["file"];//表單中取得分塊文件
//if (data != null)//為null可能是暫停的那一瞬間
//{
data.SaveAs(filePath);//報錯
//}
return Json(new { erron = 0 });//Demo,隨便返回了個值,請勿參考
}
6.實現(xiàn)效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
深入Lumisoft.NET組件與.NET API實現(xiàn)郵件發(fā)送功能的對比分析
本篇文章對Lumisoft.NET組件與.NET API實現(xiàn)郵件發(fā)送的功能兩者進行了深入的對比分析。需要的朋友參考下2013-05-05
.NET?Core使用Autofac容器的DI依賴注入,IOC控制反轉及AOP切面編程
本文詳細講解了.NET?Core使用Autofac容器的DI依賴注入,IOC控制反轉及AOP切面編程,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02

