百度webupload網址:http://fex.baidu.com/webuploader/javascript
引入js和csscss
<script src="../../Content/webuploader.js"></script>
<link href="../../Content/webuploader.css" rel="stylesheet" />html
頁面html代碼java
<div id="uploader" class="wu-example">
<!--用來存放文件信息-->
<div id="thelist" class="uploader-list"></div>
<div class="btns">
<div id="picker">選擇文件</div>
<button id="ctlBtn" class="btn btn-default">開始上傳</button>
</div>
</div>web
初始化webupload.jsjson
<script type="text/javascript">
// 文件上傳
jQuery(function () {
var $ = jQuery,
$list = $('#thelist'),
$btn = $('#ctlBtn'),
state = 'pending',
uploader;app
uploader = WebUploader.create({ui
// 不壓縮image
resize: false,url
// swf文件路徑
swf: '/content/Uploader.swf',spa
// 文件接收服務端。
server: '/Webupload/Process', (這是在後臺寫的接收前臺傳送的文件方法)
// 選擇文件的按鈕。可選。
// 內部根據當前運行是建立,多是input元素,也多是flash.
pick: '#picker',
chunked :true,
chunkSize: 5242880,(分佈上傳 一次5mb)
threads :1(這個是關鍵 若是不限制同時上傳的數目 會致使後臺接受的分片錯亂 好比按正常的分片第一片應該是開頭 但接收的可能就變成第三片從而順序錯亂 這是因爲百度webuploader默認容許同時最大上傳進程數爲3個 因此會致使接受順序錯亂從而重組發生錯誤)
});
// 當有文件添加進來的時候
uploader.on('fileQueued', function (file) {
$list.append('<div id="' + file.id + '" class="item">' +
'<div class="info">' + file.name + '</div>' +
'<div class="state">等待上傳...</div>' +
'<div class="download" style="display:none;"></div>' +
'<div class="del"></div>' +
'</div>');
});
uploader.on('uploadProgress', function (file, percentage) {
var $li = $('#' + file.id),
$percent = $li.find('.progress .bar');
// 避免重複建立
if (!$percent.length) {
$percent = $('<span class="progress">' +
'<span class="percentage"><span class="text"></span>' +
'<span class="bar" role="progressbar" style="width: 0%">' +
'</span></span>' +
'</span>').appendTo($li).find('.bar');
}
$li.find('div.state').text('上傳中');
$li.find("span.text").text(Math.round(percentage * 100) + '%');//顯示上傳進度
$percent.css('width', percentage * 100 + '%');
});
uploader.on('uploadSuccess', function (file) {
$('#' + file.id).find('div.state').text('已上傳');
});
uploader.on('uploadError', function (file) {
$('#' + file.id).find('div.state').text('上傳出錯');
});
uploader.on('uploadComplete', function (file) {
$('#' + file.id).find('.progress').fadeOut();
});
uploader.on('all', function (type) {
if (type === 'startUpload') {
state = 'uploading';
} else if (type === 'stopUpload') {
state = 'paused';
} else if (type === 'uploadFinished') {
state = 'done';
}
if (state === 'uploading') {
$btn.text('暫停上傳');
} else {
$btn.text('開始上傳');
}
});
$btn.on('click', function () {
if (state === 'uploading') {
uploader.stop(true);//不加ture暫停上傳沒用!
} else {
uploader.upload();
}
});
});
</script>
後臺接收上傳的文件的代碼(接收接口) 內有上傳分佈接受
public class WebuploadController : Controller
{
//
// GET: /Webupload/
public WebuploadController()
{
var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
urlPath = "/PHP";
}
static string urlPath = string.Empty;
static string guid = Guid.NewGuid().ToString("N");
static int num = 1;
public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
{
string filePathName = string.Empty;
string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
if (Request.Files.Count == 0)
{
return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失敗" }, id = "id" });
}
string ex = Path.GetExtension(file.FileName);
filePathName = guid+"_"+num + ex;//分佈接受(創建每一個接受分段的名字)
num++;
if (!System.IO.Directory.Exists(localPath))
{
System.IO.Directory.CreateDirectory(localPath);
}
file.SaveAs(Path.Combine(localPath, filePathName));//寫入文件夾中
int total = size / 5242880;//判斷有多少個分塊
if(size%5242880>0)
{
total += 1;
}
//重組
if (num > total)
{
chongzu(total, ex, guid, localPath);
}
return Json(new
{
jsonrpc = "2.0",
id = "id",
filePath = urlPath + "/" + filePathName
});
}
//重組(分片數目,接受視頻類型,存儲視頻的名字,存放的地址)
public void chongzu(int total, string ex, string guid, string localPath)
{
try
{
string fileurl = Path.Combine(localPath, guid + ex);
var fs = new FileStream(fileurl, FileMode.Create);
for (int i = 1; i <= total; ++i)
{
string part = Path.Combine(localPath, guid + "_" + i + ex);
var bytes = System.IO.File.ReadAllBytes(part);
fs.Write(bytes, 0, bytes.Length);
bytes = null;
System.IO.File.Delete(part);
}
fs.Close();
filenameone = guid + ex;
}
catch (Exception e) { Response.Write("<script>alert('視頻重組失敗') </script>"); }
finally { num = 1; guid = Guid.NewGuid().ToString("N"); //爲了下個視頻重置num 和 guid }
}
}
對於上傳大的文件須要在配置文件中改iis配置
<system.web>
<httpRuntime maxRequestLength="999999999" />//用戶上傳文件最大致積
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
</system.webServer>