原生html上传文件

<%@ WebHandler Language="C#" Class="FileHandler" %>

using System;
using System.Web;
using System.IO;

public class FileHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file =context.Request.Files[0];
                file.SaveAs(context.Server.MapPath("upload/"+Guid.NewGuid()+Path.GetExtension(file.FileName)));
                context.Response.Write("上传成功!");
            }
            else
            {
                context.Response.Redirect("FileUpload.html");
            }
        }
        catch(Exception ex)
        {
            context.Response.Write(ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

posted @ 2018-03-15 09:13  你的斗志并没有失去  阅读(437)  评论(0编辑  收藏  举报