swf上傳控制項在asp.net和MVC中的運用

來源:互聯網
上載者:User

找到資料包

沒有的話就先去我的資源空間去下載

http://download.csdn.net/source/1860028
裡麵包含3種檔案
(1)Ashx
(2)SWF
(3)swfuoload.js
(4)XPButtonNoText_160x22.png圖片
把他複製到項目中,具體是哪,可以自己根據需求放

 使用方法:
(1):引入swfupload.js檔案
 <script type="text/javascript" src="資料包/swfupload.js"></script>

(2):複製下面代碼到項目中。
    <script type="text/javascript">
        var swfu;
        window.onload = function() {
            swfu = new SWFUpload({
                // Backend Settings
                upload_url: "資料包/Ashx/Upload.ashx",
                post_params: {
                    "ASPSESSID": "<%=Session.SessionID %>"
                },

                // File Upload Settings
                file_size_limit: "5 MB",
                file_types: "*.jpg;*.png;*.gif;*.bmp;*.jpeg",
                file_types_description: "JPG Images",

                // Event Handler Settings
                file_dialog_complete_handler: function(numFilesSelected, numFilesQueued) { if (numFilesQueued > 0) this.startUpload(); },
                upload_success_handler: function(file, responseText) {
                    alert("圖片上傳成功。上傳之前的名稱為:" + file.name + "上傳之後的檔案名稱為:" + responseText);
                    document.getElementById("path").value = file.name;
                },

                // Button settings
                button_image_url: "~/資料包/XPButtonNoText_160x22.png",
                button_placeholder_id: "spanButtonPlaceholder",
                button_width: 160,
                button_height: 22,
                button_text: '點擊瀏覽上傳(&lt;3M)',
                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: "資料包/Swf/swfupload.swf", // Relative to this file

                // Debug Settings
                debug: false
            });
        }
    </script>
 其中:
  upload_url:為ashx的路徑
  file_size_limit: 上傳的最大限制
  file_types:上傳的類型
  file_types_description:上傳的檔案類型說明
  
  upload_success_handler: function(file, responseText) {}檔案上傳成功之後的事情。可以根據需求在裡面寫內容
  其中參數 file.name 檔案上傳前的名稱
  responseText 為 檔案上傳之後的名稱
  button_placeholder_id: 控制項的ID
  flash_url 為swfupload.swf的路徑
  其他的都很簡單,就不一一介紹了。
  請根據自己的具體情況,修改上面的參數
  
  
(3)找到ashx檔案夾中的Upload.ashx
 找到這句代碼upload.SaveAs(Path.Combine(context.Server.MapPath("upload"), name));
 這裡儲存的絕對路徑。context.Server.MapPath("upload")的意思是項目Upload.ashx當前的路徑的upload的檔案夾下,name為檔案名稱
 我們再跟目錄下建一個檔案夾upload用來存放上傳的檔案。
 更改 upload.SaveAs(Path.Combine(context.Server.MapPath("../../upload"), name));
 這樣上傳的檔案就被儲存在upload檔案夾下了。
 
 (4)設定設定檔 web.config 
 在</system.web>節點下面添加
 <location path="資料包/Ashx/Upload.ashx">
  <system.web>
   <httpRuntime maxRequestLength="3100" executionTimeout="300"/>
  </system.web>
  </location>
  其中maxRequestLength="3100" 為最大限制3M  逾時為executionTimeout="300"
  
 (5)使用的時候加上下面的代碼:
       <div style="float:left"><input type="text" id="Path" /></div>
       <div style="float:left"><div id="spanButtonPlaceholder"></div></div>

 到這裡上傳控制項就已經完成了。點擊運行,你學會了嗎。
 
( MVC的和這個很類似,就不再做詳細說明了。注意,在MVC中 <input type="text" id="path" />ID不能為path關鍵字

還有MVC的Content檔案夾下的目錄如果要用相對路徑的話,最好是先返回到Content檔案夾,再往下找)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.