檔案上傳之——用SWF外掛程式實現檔案非同步上傳

來源:互聯網
上載者:User

標籤: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外掛程式實現檔案非同步上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.