<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>SWFUpload上傳大文件</title> <script src="js/jquery-1.6.1.min.js" type="text/javascript"></script> <script type="text/javascript" src="swfupload/swfupload.js"></script> <script type="text/javascript"> function fileQueueError(file, errorCode, message) { try { var v = $("#mes").html(); var str = "觸發fileQueueError事件,參數file:" + file + ",參數errorCode:" + errorCode + ",參數message:" + message; $("#mes").html(v + str + "<br />"); } catch (ex) { this.debug(ex); } } function fileDialogComplete(numFilesSelected, numFilesQueued) { try { if (numFilesQueued > 0) { /* var v = $("#mes").html(); var str = "觸發fileDialogComplete事件,參數numFilesSelected:" + numFilesSelected + ",參數numFilesQueued:" + numFilesQueued; $("#mes").html(v + str + "<br />");*/ this.startUpload(); } } catch (ex) { this.debug(ex); } } function uploadProgress(file, bytesLoaded) { try { var percent = Math.ceil((bytesLoaded / file.size) * 100); // 計算百分比 $("#filename").html(file.name); $("#filesize").html(file.size); $("#ysc").html(bytesLoaded); $("#bfb").html(percent); /* var v = $("#mes").html(); var str = "觸發uploadProgress事件,參數file:" + file + ",參數bytesLoaded:" + bytesLoaded; $("#mes").html(v + str + "<br />");*/ } catch (ex) { this.debug(ex); } } function uploadSuccess(file, serverData) { try { /* var v = $("#mes").html(); var str = "觸發uploadSuccess事件,參數file:" + file + ",參數serverData:" + serverData; $("#mes").html(v + str + "<br />");*/ } catch (ex) { this.debug(ex); } } function uploadComplete(file) { try { /* I want the next upload to continue automatically so I'll call startUpload here */ if (this.getStats().files_queued > 0) { this.startUpload(); } var v = $("#mes").html(); var str = "觸發uploadComplete事件,參數file:" + file+",<span style='color:red;font-weight:bold;'>文件"+file.name+"上傳完成</span>"; $("#mes").html(v + str + "<br />"); } catch (ex) { this.debug(ex); } } function uploadError(file, errorCode, message) { try { switch (errorCode) { case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED: try { } catch (ex1) { this.debug(ex1); } break; case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED: try { } catch (ex2) { this.debug(ex2); } case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED: break; default: alert(message); break; } var v = $("#mes").html(); var str = "觸發uploadError事件,參數file:" + file + ",參數errorCode:" + errorCode + ",參數message:" + message; $("#mes").html(v + str + "<br />"); } catch (ex3) { this.debug(ex3); } } </script> <script type="text/javascript"> var swfu; window.onload = function() { swfu = new SWFUpload({ // 基本設置 upload_url: "upload.aspx", post_params: { "ASPSESSID": "<%=Session.SessionID %>" }, // 上傳文件設置 file_size_limit: "100 MB", file_types: "*.jpg;*.avi;*.rar;*.mp4", file_types_description: "支持上傳的格式", file_upload_limit: "0", // 0表示不限制選擇文件的數量 // 定義事件 file_queue_error_handler: fileQueueError, file_dialog_complete_handler: fileDialogComplete, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: uploadSuccess, upload_complete_handler: uploadComplete, // 按鈕設置 button_image_url: "images/XPButtonNoText_160x22.png", button_placeholder_id: "spanButtonPlaceholder", button_width: 160, button_height: 22, button_text: '<span class="button">選擇文件 <span class="buttonSmall">(最大 100 MB)</span></span>', button_text_style: '.button { font-family: 微軟雅黑, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 12pt; }', button_text_top_padding: 0, button_text_left_padding: 5, // Flash設置 flash_url: "swfupload/swfupload.swf", // 是否開啓調試,true是,false否 debug: false }); } </script> </head> <body> <form id="form1" runat="server"> <h1> SWFUpload上傳大文件</h1> <div id="swfu_container" style="margin: 0px 10px;"> <div> <span id="spanButtonPlaceholder"></span> </div> <div style="height: 75px;"> 文件名稱:<span id="filename"></span><br /> 文件大小:<span id="filesize"></span> 字節<br /> 已上傳:<span id="ysc"></span> 字節<br /> 上傳百分比:<span id="bfb"></span>% </div> </div> <hr /> <div id="mes"> </div> </form> </body> </html>
// 獲取數據 HttpPostedFile hpf = Request.Files["Filedata"]; //創建今天的文件夾 string d = DateTime.Now.ToString("yyyyMMdd"); string path = Server.MapPath("~/upload/" + d + "/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } hpf.SaveAs(path + hpf.FileName);