jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
<%@ WebHandler Language="C#" Class="UploadHandler" %>
 
using System;
using System.IO;
using System.Net;
using System.Web;
 
public class UploadHandler : IHttpHandler
{
 
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            string fileLogic = context.Request.QueryString["folder"]+"/";
            string strUploadPath = context.Request.PhysicalApplicationPath + context.Request.QueryString["folder"].Trim('/').Replace("/", "\\") + "\\";
            //创建路径
            if (!Directory.Exists(strUploadPath))
            {
                Directory.CreateDirectory(strUploadPath);
 
            }
            string fileNameLogic = string.Empty;
            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                HttpPostedFile postedFile = context.Request.Files[i];
                //获取扩展名
                string fileExtension = System.IO.Path.GetExtension(postedFile.FileName).ToLower();
                string fileRealName = string.Empty;
                if (fileLogic.Contains("Files"))
                {
                    fileRealName = postedFile.FileName;
                }
                else
                {
                    //string fileName = strUploadPath + Path.GetFileName(postedFile.FileName);
                    //生成随机名称
                     fileRealName = DateRndName() + fileExtension;
                }
                //相对路径文件名称
                fileNameLogic = fileLogic + fileRealName;
                
                string fileName = strUploadPath + fileRealName;
 
                if (fileName != "")
                {
                    postedFile.SaveAs(fileName);
 
                }
            }
            context.Response.Write(fileNameLogic);
            //context.Response.Write(" ");
 
        }
        catch (Exception ex)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(ex.Message);
        }
 
    }
 
     
 
    #region DateRndName()日期时间+3位数随机
    string DateRndName()
    {
        Random ra = new Random();
        DateTime d = DateTime.Now;
        string s = null, y, m, dd, h, mm, ss;
        y = d.Year.ToString();
        m = d.Month.ToString();
        if (m.Length < 2) m = "0" + m;
        dd = d.Day.ToString();
        if (dd.Length < 2) dd = "0" + dd;
        h = d.Hour.ToString();
        if (h.Length < 2) h = "0" + h;
        mm = d.Minute.ToString();
        if (mm.Length < 2) mm = "0" + mm;
        ss = d.Second.ToString();
        if (ss.Length < 2) ss = "0" + ss;
        s += y + m + dd + h + mm + ss;
        s += ra.Next(100, 999).ToString();
        return s;
    }
    #endregion
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}