首先引用文件:
js:
<script type="text/javascript">
var swfu;
var type = GetQueryString("type");
var id = GetQueryString("id");
SWFUpload.onload = function () {
var settings = {
flash_url: "swfupload/swfupload.swf",
upload_url: "Upload.ashx",
post_params: {
"ASPSESSID": "<%=Session.SessionID %>",
"type": type,
"id": id
},
file_size_limit: "0",
file_types: "*.*",
file_types_description: "视频文件",
file_upload_limit: 100,
file_queue_limit: 50,
custom_settings: {
progressTarget: "fsUploadProgress",
cancelButtonId: "btnCancel"
},
debug: false,
// Button Settings
button_placeholder_id: "spanButtonPlaceholder",
button_width: 120,
button_height: 30,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
// The event handler functions are defined in handlers.js
//swfupload_loaded_handler: swfUploadLoaded,
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
// SWFObject settings
minimum_flash_version: "9.0.28",
swfupload_pre_load_handler: swfUploadPreLoad,
// swfupload_load_failed_handler: swfUploadLoadFailed
};
swfu = new SWFUpload(settings);
}
//获取地址栏参数
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
</script>
aspx:
<body>
<div id="content">
<form id="form1" action="index.aspx" method="post" enctype="multipart/form-data">
<div id="divSWFUploadUI">
<p>
<span id="spanButtonPlaceholder"></span>
<input id="btnUpload" type="button" value="选择文件" style="width: 120px; height: 30px; font-size: 14pt; font-weight: bold;" />
<b id="divStatus" style="color: red;">请选择要上传的文件</b>
<input id="btnCancel" type="button" value="Cancel All Uploads" disabled="disabled" style="display: none; margin-left: 2px; height: 22px; font-size: 8pt;" />
</p>
<br style="clear: both;" />
<div class="fieldset flash" id="fsUploadProgress">
<span class="legend">上传队例</span>
</div>
</div>
</form>
</div>
</body>
ashx:
public void ProcessRequest(HttpContext context)
{
var Response = context.Response;
var Request = context.Request;
var Session = context.Session;
string type = context.Request["type"];
string id = context.Request["id"];
ArtificialDataTitleDomain art = new ArtificialDataTitleDomain();
ArtificialDataTitleEntity Inspect = new ArtificialDataTitleEntity();
TestingMachineTitleDomain tmt = new TestingMachineTitleDomain();
TestingMachineTitleEntity Repair = new TestingMachineTitleEntity();
string s_rpath = System.Web.HttpContext.Current.Server.MapPath("") + "\\" + "doc";
string Datedir = DateTime.Now.ToString("yy-MM-dd") + " - " + id;
string updir = s_rpath + "\\" + Datedir;
Response.CacheControl = "no-cache";
if (Request.Files.Count > 0)
{
try
{
for (var j = 0; j < Request.Files.Count; j++)
{
var uploadFile = Request.Files[j];
if (uploadFile.ContentLength <= 0) continue;
if (!Directory.Exists(updir))
{
Directory.CreateDirectory(updir);
}
var extname = Path.GetExtension(uploadFile.FileName);
var fullname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
var filename = uploadFile.FileName;
uploadFile.SaveAs(string.Format("{0}\\{1}", updir, filename));
}
List<string> lst = getFileName(Datedir);
string allFileName = "";
if (lst.Count > 0)
{
for (int i = 0; i < lst.Count; i++)
{
allFileName += lst[i] + ",";
}
}
if (type == "Inspect")
{
Inspect = art.FindByID(id);
Inspect.docUrl = Datedir + "*" + allFileName;
art.Update(Inspect);
}
else if (type == "Repair")
{
Repair = tmt.FindByID(id);
Repair.docUrl = Datedir + "*" + allFileName;
tmt.Update(Repair);
}
}
catch (Exception ex)
{
Response.Write("Message" + ex.ToString());
}
}
}
//此方法是我自己后期会用到下载
//而上面的方法获取的FileName只能一个个获取 所以在此循环去取路径下的文件名 比较笨 但实用
public List<string> getFileName(string url)
{
string filePath = string.Format(System.Web.HttpContext.Current.Server.MapPath("doc/{0}"), url);//路径
DirectoryInfo dir = new DirectoryInfo(filePath);
FileInfo[] fileInfo = dir.GetFiles();
List<string> fileNames = new List<string>();
foreach (FileInfo item in fileInfo)
{
fileNames.Add(item.Name);
}
return fileNames;
}
特别说明:
此方法适用于单个文件2G以下的多文件上传。下次更新超2G的多文件代码
关于swfupload里上传后上传列队里的信息会自动消失 可以自己根据需要自己设置如下属性
fileprogress.js 里的
this.setTimer(setTimeout(function () {
oSelf.disappear();
}, 10000));
注释即不消失
js:
file_size_limit: "0" -- 不限制上传文件大小
file_types: "*.*", --- 不限制文件格式
post_params: {
"ASPSESSID": "<%=Session.SessionID %>",
"type": type,
"id": id
}, --- 传值 在ashx中直接 string type = context.Request["type"]; 获取