项目结构 以及插件需要的文件如图所示
前端代码:
<!DOCTYPE html>
<html>
<head>
<title>SWFUpload</title>
<link href="~/Content/Scripts/tools/swfupload/css/default.css" rel="stylesheet" />
<script src="~/Content/Scripts/tools/jquery-1.11.3.min.js"></script>
<script src="~/Content/Scripts/tools/json2.js"></script>
<script src="~/Content/Scripts/tools/swfupload/fileprogress.js"></script>
<script src="~/Content/Scripts/tools/swfupload/handlers.js"></script>
<script src="~/Content/Scripts/tools/swfupload/swfupload.js"></script>
<script src="~/Content/Scripts/tools/swfupload/swfupload.queue.js"></script>
<script type="text/javascript">
var swfu;
window.onload = function () {
var settings = {
upload_url: "/UpLoad/UpLoading", // 上传地址
flash_url: "/Content/Scripts/tools/swfupload/swfupload.swf", // 上传图片flash
preserve_relative_urls: false,
file_post_name: "fileName", // 上传文件参数名
file_size_limit: "100 MB", // 文件大小
file_types: "*.jpg;*.png;*.gif;*.bmp;*.doc;*.docx", // 文件格式
file_types_description: "All Files",
file_upload_limit: "5",
file_queue_limit: "0",
custom_settings: {
progressTarget: "fsUploadProgress",
cancelButtonId: "btnCancel"
},
debug: true,
// Button settings
button_image_url: "/Content/Scripts/tools/swfupload/images/TestImageNoText_65x29.png", // Relative to the Flash file
button_width: "65",
button_height: "29",
button_placeholder_id: "spanButtonPlaceHolder",
button_text: '<span class="theFont">浏览</span>',
button_text_style: ".theFont { font-size: 16; }",
button_text_left_padding: 12,
button_text_top_padding: 3, // The event handler functions are defined in handlers.js
file_queued_handler: fileQueued,
file_queue_error_handler: fileQueueError,
file_dialog_complete_handler: fileDialogComplete,
upload_start_handler: uploadStart,
upload_progress_handler: uploadProgress,
upload_error_handler: uploadError,
upload_success_handler: uploadSuccess,
upload_complete_handler: uploadComplete,
queue_complete_handler: queueComplete // Queue plugin event
}; swfu = new SWFUpload(settings);
};
</script>
</head>
<body>
<div id="content">
<p>点击“浏览”按钮,选择您要上传的文档文件后,系统将自动上传并在完成后提示您。</p>
<p>请勿上传包含中文文件名的文件!</p>
<div class="fieldset flash" id="fsUploadProgress">
<span class="legend">快速上传</span>
</div>
<div id="divStatus">0 个文件已上传</div>
<div>
<span id="spanButtonPlaceHolder"></span>
<input id="btnCancel" type="button" value="取消所有上传" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;"/>
<input id="btnPase" type="button" value="暂停上传" onclick="swfu.stopQueue();" style="margin-left: 2px; font-size: 8pt; height: 29px;"/>
<input id="btnGoon" type="button" value="继续上传" onclick="swfu.startUpload();" style="margin-left: 2px; font-size: 8pt; height: 29px;"/>
</div>
</div>
</body>
</html>
后端代码:
using System;
using System.Web;
using System.Web.Mvc; namespace Notify.Controller.SwfupLoad
{
/// <summary>
/// 上传图片控制器
/// </summary>
public class UpLoadController : System.Web.Mvc.Controller
{
/// <summary>
/// 上传图片首页
/// </summary>
/// <returns>视图</returns>
public ActionResult Index()
{
return View();
} /// <summary>
/// 上传图片处理
/// </summary>
/// <param name="fileName">图片流</param>
/// <returns>结果</returns>
public ActionResult UpLoading(HttpPostedFileBase fileName)
{
if (fileName != null)
{
//创建图片新的名称
string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff");
//获得上传图片的路径
string strPath = fileName.FileName;
//获得上传图片的类型(后缀名)
string type = strPath.Substring(strPath.LastIndexOf(".", StringComparison.Ordinal) + ).ToLower();
if (ValidateImg(type))
{
//拼写数据库保存的相对路径字符串
//拼写上传图片的路径
var uppath = Server.MapPath("~/Content/Images/UpImgs/");
uppath += nameImg + "." + type;
//上传图片
fileName.SaveAs(uppath);
}
}
return Content("OK");
} /// <summary>
/// 验证上传图片类型
/// </summary>
/// <param name="imgName">图片</param>
/// <returns>结果</returns>
public bool ValidateImg(string imgName)
{
string[] imgType = { "gif", "jpg", "png", "bmp" }; int i = ;
bool blean = false; //判断是否为Image类型文件
while (i < imgType.Length)
{
if (imgName.Equals(imgType[i]))
{
blean = true;
break;
}
if (i == (imgType.Length - ))
{
break;
}
i++;
}
return blean;
}
}
}
使用svn下载
项目地址:http://code.taobao.org/svn/Notify/