標籤:response rect query rac max url 相容 handle datetime
之前提高過幾篇檔案上傳,那些都不錯。今天小編帶領大家體會一種新的上傳方法,及使用Flash外掛程式實現檔案上傳。
使用Flash的好處就是可以解決瀏覽器安全色性問題。之前我寫的一個快捷複製功能也是利用的Flash。
最近一直在用MVC,所以還是以MVC舉例;先來張:
UploadIndex2.cshtml代碼:
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>上傳</title> <script src="~/SWFUpload/swfupload.js" type="text/javascript"></script> <script src="~/SWFUpload/handlers.js" type="text/javascript"></script> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script type="text/javascript"> var swfu; window.onload = function () { swfu = new SWFUpload({ // Backend Settings upload_url: "@Url.Action("HandlerUpload2","Home")",//上傳路徑 post_params: { "ASPSESSID": "<@Session.SessionID>" }, // File Upload Settings file_size_limit: "5 MB", file_types: "*.jpg;*.gif", file_types_description: "JPG Images", file_upload_limit: 0, // Zero means unlimited // Event Handler Settings - these functions as defined in Handlers.js // The handlers are not part of SWFUpload but are part of my website and control how // my website reacts to the SWFUpload events. swfupload_preload_handler: preLoad, swfupload_load_failed_handler: loadFailed, file_queue_error_handler: fileQueueError, file_dialog_complete_handler: fileDialogComplete, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: showData, upload_complete_handler: uploadComplete, // Button settings button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",//設定按鈕圖片,注意要是連續的四張 button_placeholder_id: "spanButtonPlaceholder",//設定按鈕id button_width: 160, button_height: 22, button_text: ‘<span class="button">請選擇上傳圖片 <span class="buttonSmall">(5 MB Max)</span></span>‘, button_text_style: ‘.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }‘, button_text_top_padding: 1, button_text_left_padding: 5, // Flash Settings flash_url: "/SWFUpload/swfupload.swf", // Relative to this file flash9_url: "/SWFUpload/swfupload_FP9.swf", // Relative to this file custom_settings: { upload_target: "divFileProgressContainer" }, // Debug Settings debug: false }); } //上傳成功以後執行該方法. function showData(file, serverData) { $("#imgSrc").attr("src", serverData); } </script></head><body> <form id="form1"> <div id="content"> <div id="swfu_container" style="margin: 0px 10px;"> <div> <span id="spanButtonPlaceholder"></span> <!--注意:這個ID要和上面的一致--> </div> <img id="imgSrc" /><!---在這裡顯示上傳的圖片,這個外掛程式採用的為非同步上傳--> </div> </div> </form></body></html>View Code
後端代碼:
public ActionResult UploadIndex2() { return View(); } [HttpPost] public ActionResult HandlerUpload2() { HttpPostedFileBase file = Request.Files["Filedata"];//擷取檔案資料. string fileName = Path.GetFileName(file.FileName);//擷取檔案名稱. string fileExt = Path.GetExtension(fileName); //擷取檔案類型 string fullDir = ""; if (fileExt == ".jpg") { string dir = "/UploadImage/" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "/"; Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(dir)));//如果沒有檔案目錄,建立. fullDir = dir + OnLineBooks.App.Common.WebCommon.GetStreamMD5(file.InputStream) + fileExt;//構建了一個完整路徑,並且以檔案流的MD5值作為檔案的名稱,解決了檔案名稱重名問題。 file.SaveAs(Server.MapPath(fullDir));//檔案儲存 Response.Write(fullDir); } return Content(fullDir);//返回圖片路徑 }View Code
SWFUpload檔案下載連結:http://pan.baidu.com/s/1c2xIf5Y 密碼:wphd
檔案上傳之——用SWF外掛程式實現檔案非同步上傳