本文給大家介紹的是asp.net實現檔案無重新整理上傳的2種方法,分別是使用swfupload外掛程式和uploadify外掛程式,講述的十分細緻全面,附上樣本,有需要的小夥伴可以參考下。
遇到上傳檔案的問題,結合之前用到過的swfUpload,又找了一個無重新整理上傳檔案的jquery外掛程式uploadify,寫篇部落格記錄一下分別介紹這兩個的實現方法
swfUpload 匯入swfUpload的開發包 添加js引用,引用swfUpload.js與handler.js檔案,如果對swfUpload不瞭解、有疑問可以看看這篇文章 頁面初始化
修改handler.js檔案中 上傳成功的事件,serverData是伺服器端的響應
Uploadify 匯入uploadify開發包,從官網下載,官網文檔,中文文檔,官網樣本 添加js與css的引用,jquery.uploadify.js 、uploadify.css
(註:在css中引用uploadify-cancel.png圖片檔案的路徑是可能不正確,可以在uploadify.css檔案中自己變更)
頁面初始化
頁面初始化時,可以指定許多設定,並對上傳成功的事件進行重載,data表示伺服器端的響應
伺服器端上傳處理常式
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//uploadify初始化 $(function () { $('#file_upload').uploadify({ //指定swf 'swf': '/uploadify/uploadify.swf', //伺服器端處理常式 'uploader': '/Admin/UploadFileHandler.ashx', //按鈕文本 buttonText: '上傳附件', //檔案類型 fileTypeExts: "*.zip;*.rar;*.doc;*.docx;*.xls;*xlsx", onUploadSuccess: OnFileUploadSuccess }); }); function OnFileUploadSuccess(file, data, response) { //伺服器端響應 if (data == 'noPermission') { alert('沒有上傳許可權'); } if (data == 'Error') { alert('上傳失敗'); } else if (response) { alert('上傳成功~~~'); $("#filePath").val(data); } } uploadify |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
/// <summary> /// 上傳檔案 /// </summary> public class UploadFileHandler : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //驗證上傳許可權 if (context.Session["User"] == null) { context.Response.Write("no permission"); context.Response.End(); return; } try { //擷取上傳檔案 //Filedata是用戶端已經定義好的,如果想要更改,更改js檔案中的配置 HttpPostedFile image_upload = context.Request.Files["Filedata"]; //擷取副檔名 string fileExt = System.IO.Path.GetExtension(image_upload.FileName).ToLower(); //驗證副檔名是否符合要求,是否是允許的圖片格式 if (!FileTypes.IsAllowed(fileExt)) { return; } //目前時間字串 string timeString = DateTime.Now.ToString("yyyyMMddHHmmssfff"); //儲存虛擬路徑構建 string path = "/Upload/" + timeString + fileExt; //擷取、構建要上傳檔案的實體路徑 string serverPath = context.Server.MapPath("~/" + path); //儲存圖片到伺服器 image_upload.SaveAs(serverPath); //輸出儲存路徑 context.Response.Write(path); } catch (Exception ex) { context.Response.Write("Error"); //記錄日誌 new Common.LogHelper(typeof(UploadFileHandler)).Error(ex); } } public bool IsReusable { get { return false; } } } public static class FileTypes { private static List<string> allowedFileTypes = new List<string>(); //擷取允許json設定檔 private static string jsonFilePath = Common.PathHelper.MapPath("~/AllowedFileTypes.json"); /// <summary> /// 允許的檔案類型 /// </summary> public static List<string> AllowedFileTypes { get { return allowedFileTypes; } set { allowedFileTypes = value; } } /// <summary> /// 靜態構造方法 /// </summary> static FileTypes() { LoadFileTypesFromJson(); } /// <summary> /// 從json檔案中讀取允許上傳的檔案類型 /// </summary> private static void LoadFileTypesFromJson() { string types = File.ReadAllText(jsonFilePath); AllowedFileTypes = Common.ConverterHelper.JsonToObject<List<string>>(types); } /// <summary> /// 當添加允許檔案類型時,更新到json檔案 /// </summary> public static void FileTypesToJson() { string types = Common.ConverterHelper.ObjectToJson(AllowedFileTypes); File.WriteAllText(jsonFilePath, types); } /// <summary> /// 新增允許上傳副檔名 /// </summary> /// <param name="newFileType"></param> public static void AddNewFileType(string newFileType) { AllowedFileTypes.Add(newFileType); FileTypesToJson(); } /// <summary> /// 判斷某種檔案類型是否允許上傳 /// </summary> /// <param name="fileExt">副檔名</param> /// <returns>是否允許上傳<code>true</code>允許上傳</returns> public static bool IsAllowed(string fileExt) { foreach (string item in AllowedFileTypes) { if (fileExt.Equals(fileExt)) { return true; } } return false; } } |
以上所述就是本文的全部內容了,希望大家能夠喜歡。