asp.net多檔案上傳

來源:互聯網
上載者:User

標籤:

用的falsh外掛程式swfupload

1、html頁面中添加引用:

1 <script src="/Scripts/uploadFile/callbacks.js" type="text/javascript"></script>2 <script src="/Scripts/uploadFile/fileprogress.js" type="text/javascript"></script>3 <script src="/Scripts/uploadFile/swfupload.js" type="text/javascript"></script>4 <script src="/Scripts/uploadFile/swfupload.queue.js" type="text/javascript"></script>

 

2、添加JS配置

  需要更改的參數:upload_url(後台接收檔案的地址)、flash_url(flash檔案在伺服器中的地址)、flash9_url、button_image_url(選擇檔案按鈕的背景圖片)

 1  <script type="text/javascript"> 2         $(function () { 3             var swfupload = null; 4             var settings = { 5                 upload_url: ‘/ashx/uploadFile/AddFile.ashx‘,           //附件上傳伺服器位址 6                 file_post_name: ‘file‘,      //向後台提交的表單名 7                 flash_url: "/Scripts/uploadFile/swfupload.swf", 8                 flash9_url: "/Scripts/uploadFile/swfupload_fp9.swf", 9                 post_params: { "PHPSESSID": "<?php echo session_id(); ?>" }, //解決session丟失問題10                 file_size_limit: "50 MB",                                 //檔案大小限制,此處僅是前端flash選擇時候的限制,具體還需要和後端結合判斷11                 file_types: "*.xls;*.xlsx", //允許的副檔名,多個副檔名之間用分號隔開,支援*萬用字元12                 file_types_description: "Excel",                      //副檔名描述13                 file_upload_limit: 0,                                   //單次可同時上傳的檔案數目14                 file_queue_limit: 0,                                      //隊列中可同時上傳的檔案數目15                 custom_settings: {                                         //自訂設定,使用者可在此向伺服器傳遞自訂變數16                     startUploadId: "btn_addFile"17                 },18                 debug: false,19 20                 // 按鈕設定21                 button_image_url: "/Images/btn_swfupload.png",22                 button_width: "150",23                 button_height: "30",24                 button_placeholder_id: "spanButtonPlaceHolder",25                 button_text: ‘<span class="theFont" style="border:1px #ccc solid">請選擇檔案...</span>‘,26                 button_text_style: ".theFont { font-size:15px;background-color:white;}",27                 button_text_left_padding: 2,28                 button_text_top_padding: 5,29 30                 // 所有回呼函數 in handlers.js31                 swfupload_preload_handler: preLoad,32                 swfupload_load_failed_handler: loadFailed,33                 file_queued_handler: fileQueued,34                 file_queue_error_handler: fileQueueError,35                 file_dialog_start_handler: function () {36                     this.cancelUpload();37                 },38                 //選擇檔案完成回調39                 file_dialog_complete_handler: function (numFilesSelected, numFilesQueued) {40                     var me = this;        //此處的this是swfupload對象 41                     if (numFilesQueued > 0) {42                         var start = document.getElementById(this.customSettings.startUploadId);43                         //start.style.display = "";44                         start.onclick = function () {45                             me.startUpload();46                         }47                     }48                 },49                 upload_start_handler: function () {50                     var ddlSeller = $(‘#ddlSeller‘).val();51                     this.setPostParams({52                         ‘type‘: ddlSeller53                     });54                 },55                 upload_progress_handler: uploadProgress,56                 upload_error_handler: uploadError,57                 upload_success_handler: function (file, serverData) {58                      //上傳成功後的回調方法
73 },74 //上傳完成回調75 upload_complete_handler: uploadComplete,76 //隊列完成回調77 queue_complete_handler: function (numFilesUploaded) {78 }79 }80 swfupload = new SWFUpload(settings);81 });88 89 90 </script>

3、html中必須的元素有:

<span id="spanButtonPlaceHolder"></span>        這個是顯示‘選擇檔案’按鈕

<label id="label_file_Name" ></label>       這個是選擇檔案後,用來顯示  選擇的檔案名稱

<input type="button" class="btn btn-primary btn-large" style="" value="匯入" id="btn_test"/>   這個是用來做一些校正性判斷
<input type="button" class="btn btn-primary btn-large" style="display:none;" value="匯入" id="btn_addFile"/>  這個是真正觸發上傳檔案的按鈕,會向伺服器post

 

4、需要用到的事件方法
   1)選擇檔案後的方法:file_dialog_complete_handler   不用修改;

   2)上傳檔案成功後的方法: upload_success_handler  根據自己的業務需要,變更

5、其他的一些JS校正性判斷

 1 $(function () { 2         $(‘#btn_test‘).click(function () { 3             var label_file_Name = $(‘#label_file_Name‘).html(); 4             if (label_file_Name.length < 1) { 5                 alert(‘請選擇檔案!‘); 6                 return false; 7             } 8             if (confirm("確認上傳?")) { 9                 $(‘#btn_addFile‘).click();  10                 $(‘#btn_test‘).hide();              11             }12 13         });14 15 16 17     });

 

 

外掛程式:http://pan.baidu.com/s/1gdhgUyf 

 

asp.net多檔案上傳

聯繫我們

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