本人在2010年時使用swfupload為核心進行檔案的批量上傳的解決方案。見文章:WEB版一次選擇多個檔案進行批量上傳(swfupload)的解決方案。
本人在2013年時使用plupload為核心進行檔案的批量上傳的解決方案。見文章:WEB版一次選擇多個檔案進行批量上傳(Plupload)的解決方案
最近在學習百度的開源上傳元件WebUploader,寫了一些示例以記錄。WebUploader的缺點是沒有一個比較好的現成的界面,這個界面需要自己去實作。自由度高了一些。
WebUploader是由Baidu WebFE(FEX)團隊開發的一個簡單的以HTML5為主,FLASH為輔的現代檔案上傳元件。在現代的浏覽器裡面能充分發揮HTML5的優勢,同時又不摒棄主流IE浏覽器,沿用原來的FLASH運作時,相容IE6+,iOS 6+, android 4+。兩套運作時,同樣的調用方式,可供使用者任意選用。
關于WebUploader的功能說明:
分片、并發
分片與并發結合,将一個大檔案分割成多塊,并發上傳,極大地提高大檔案的上傳速度。
當網絡問題導緻傳輸錯誤時,隻需要重傳出錯分片,而不是整個檔案。另外分片傳輸能夠更加實時的跟蹤上傳進度。
預覽、壓縮
支援常用圖檔格式jpg,jpeg,gif,bmp,png預覽與壓縮,節省網絡資料傳輸。
解析jpeg中的meta資訊,對于各種orientation做了正确的處理,同時壓縮後上傳保留圖檔的所有原始meta資料。
多途徑添加檔案
支援檔案多選,類型過濾,拖拽(檔案&檔案夾),圖檔粘貼功能。
粘貼功能主要展現在當有圖檔資料在剪切闆中時(截屏工具如QQ(Ctrl + ALT + A), 網頁中右擊圖檔點選複制),Ctrl + V便可添加此圖檔檔案。
HTML5 & FLASH
相容主流浏覽器,接口一緻,實作了兩套運作時支援,使用者無需關心内部用了什麼核心。
同時Flash部分沒有做任何UI相關的工作,友善不關心flash的使用者擴充和自定義業務需求。
MD5秒傳
當檔案體積大、量比較多時,支援上傳前做檔案md5值驗證,一緻則可直接跳過。
如果服務端與前端統一修改算法,取段md5,可大大提升驗證性能,耗時在20ms左右。
易擴充、可拆分
采用可拆分機制, 将各個功能獨立成了小元件,可自由搭配。
采用AMD規範組織代碼,清晰明了,友善進階玩家擴充。
一: 下面就是利用WebUploader元件,讓客戶一次選擇多個檔案,然後将選擇的檔案上傳到伺服器的批量檔案解決方案。
讓我們先來看看用戶端的界面效果圖。(多選檔案,批量上傳,上傳進度顯示)
1) 顯示界面:

圖1
2) 進行多檔案選擇:
圖2
3) 上傳進度顯示
圖3
圖4
說明:見上圖WebUploader預設是3個線程一起并發上傳檔案。如果需要增加并發數,或是減少并發數。可以修改threads屬性,這個屬性就是允許的最大上傳并發數。
3) 上傳成功顯示
圖5
二:具體的代碼與操作步驟:
第一步,要完成下面的示例,必須先準備好WebUploader元件。
1) WebUploader:大家可以通路WebUploader官方網站:http://fex.baidu.com/webuploader/download.html,在這個網站上可以下載下傳到元件與demo。
第二步,這個示例的目錄結構如圖:
1.主要目錄結構
2. 此示例中用到的js腳本檔案與css檔案。
第三步,前台部分準備客戶操作的WEB界面,如下[WebUploaderFileByBaidu2.aspx、UploaderFileByBaidu.ashx]
1) 前台用戶端代碼,其中WebUploaderFileByBaidu2.aspx的代碼如下,WebUploaderFileByBaidu2.aspx.cs檔案中隻使用預設的代碼,不用添加任何代碼。
Html代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebUploaderFileByBaidu2.aspx.cs" Inherits="WebApplication1.WebUploaderFileByBaidu2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>WebUploader檔案上傳示例</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<link href="Scripts/webuploader/webuploader.css" target="_blank" rel="external nofollow" rel="stylesheet" />
<script type="text/javascript" src="Scripts/webuploader/webuploader.min.js"></script>
<link href="https://cdn.bootcss.com/bootstrap/3.0.3/css/bootstrap.min.css" target="_blank" rel="external nofollow" rel="stylesheet"/>
<link href="Scripts/style.css" target="_blank" rel="external nofollow" rel="stylesheet" />
<link href="Scripts/demo.css" target="_blank" rel="external nofollow" rel="stylesheet" />
<link href="Scripts/font-awesome.css" target="_blank" rel="external nofollow" rel="stylesheet" />
<script type="text/javascript">
var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
// 檔案上傳
jQuery(function () {
var $ = jQuery,
$list = $('#fileList'),
$btn = $('#ctlBtn'),
state = 'pending',
uploader;
uploader = WebUploader.create({
// 不壓縮image
resize: false,
// swf檔案路徑
swf: applicationPath + 'Script/webuploader/Uploader.swf',
// 檔案接收服務端。
server: 'UploaderFileByBaidu.ashx',
// 選擇檔案的按鈕。可選。
// 内部根據目前運作是建立,可能是input元素,也可能是flash.
pick: '#picker'
});
// 當有檔案添加進來的時候
uploader.on('fileQueued', function (file) {
$list.append('<div background-color: #f5f5f5; color: #000000;">' + file.id + '" class="item">' +
'<h4 class="info">' + file.name + '</h4>' +
'<p class="state">等待上傳...</p>' +
'</div>');
});
// 檔案上傳過程中建立進度條實時顯示。
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('p.state').text('上傳中');
$percent.css('width', percentage * 100 + '%');
});
uploader.on('uploadSuccess', function (file) {
$('#' + file.id).find('p.state').text('已上傳');
});
uploader.on('uploadError', function (file) {
$('#' + file.id).find('p.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();
} else {
uploader.upload();
}
});
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="col-md-10">
<div class="row">檔案上傳示例:</div>
<div class="row">
<div id="uploader" class="wu-example">
<!--用來存放檔案資訊-->
<div id="fileList" class="uploader-list"></div>
<div class="btns">
<div id="picker" class="btn btn-primary">選擇檔案</div>
</div>
</div>
</div>
<div class="row">
</div>
<div class="row"> <button id="ctlBtn" class="btn btn-default">開始上傳</button></div>
</div>
<div>
</div>
</div>
</body>
</html>
以上代碼最後的顯示結果如下圖:
圖8.
2) 接收前端使用者上傳的檔案,并把檔案儲存到指定目錄中。UploaderFileByBaidu.ashx檔案中使用預設的代碼,不需要添加任何代碼。UploaderFileByBaidu.ashx.cs檔案的代碼如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace WebApplication1
{
/// <summary>
/// UploaderFileByBaidu 的摘要說明
/// </summary>
public class UploaderFileByBaidu : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentEncoding = Encoding.UTF8;
if (context.Request["REQUEST_METHOD"] == "OPTIONS")
{
context.Response.End();
}
SaveFile();
}
/// <summary>
/// 檔案儲存操作
/// </summary>
/// <param name="basePath"></param>
private void SaveFile(string basePath = "~/Upload/Images/")
{
basePath = FileHelper.GetUploadPath();
string Datedir = DateTime.Now.ToString("yy-MM-dd");
string updir = basePath + "\\" + Datedir;
string extname = string.Empty;
string fullname = string.Empty;
string filename = string.Empty;
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
if (files.Count == 0)
{
var result = "{\"jsonrpc\" : \"2.0\", \"error\" : \"儲存失敗\",\"id\" : \"id\"}";
System.Web.HttpContext.Current.Response.Write(result);
}
if (!Directory.Exists(updir))
Directory.CreateDirectory(updir);
var suffix = files[0].ContentType.Split('/');
var _suffix = suffix[1].Equals("jpeg", StringComparison.CurrentCultureIgnoreCase) ? "" : suffix[1];
var _temp = System.Web.HttpContext.Current.Request["name"];
if (!string.IsNullOrEmpty(_temp))
{
filename = _temp;
}
else
{
Random rand = new Random(24 * (int)DateTime.Now.Ticks);
filename = rand.Next() + "." + _suffix;
}
fullname = string.Format("{0}\\{1}",updir , filename);
files[0].SaveAs(fullname);
var _result = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"" + filename + "\"}";
System.Web.HttpContext.Current.Response.Write(_result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
第四步:檔案輔助類
/// <summary>
///FileHelper 的摘要說明
/// </summary>
public class FileHelper
{
public FileHelper()
{
//
//TODO: 在此處添加構造函數邏輯
//
}
/// <summary>
/// 擷取上傳目錄
/// </summary>
/// <returns></returns>
public static string GetUploadPath()
{
string path = HttpContext.Current.Server.MapPath("~/");
string dirname = GetDirName();
string uploadDir = path + "\\" + dirname;
CreateDir(uploadDir);
return uploadDir;
}
/// <summary>
/// 擷取臨時目錄
/// </summary>
/// <returns></returns>
public static string GetTempPath()
{
string path = HttpContext.Current.Server.MapPath("~/");
string dirname = GetTempDirName();
string uploadDir = path + "\\" + dirname;
CreateDir(uploadDir);
return uploadDir;
}
private static string GetDirName()
{
return System.Configuration.ConfigurationManager.AppSettings["uploaddir"];
}
private static string GetTempDirName()
{
return System.Configuration.ConfigurationManager.AppSettings["tempdir"];
}
public static void CreateDir(string path)
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
}
}
第五步,Web.config檔案配置資訊。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<appSettings>
<add key="uploaddir" value="upload" />
<add key="tempdir" value="temp" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
<httpRuntime maxRequestLength="2147483647" appRequestQueueLimit="1200" executionTimeout="1200"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
<system.webServer>
<security>
<requestFiltering >
<requestLimits maxAllowedContentLength="2147483647" ></requestLimits>
</requestFiltering>
</security>
</system.webServer>
</configuration>
第六步,在進行上傳之後,檔案儲存在對應的日期目錄下。如下圖。
圖 9