username@email.com
2025-04-23 d34ca25b9754e49b2d1220ff9f0de6af9b734d99
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
 
namespace CY.WebForm.Pages.business
{
    /// <summary>
    /// FileUploadHandler 的摘要说明
    /// </summary>
    public class FileUploadHandler : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
 
            try
            {
                if (context.Request.Files.Count > 0)
                {
                    HttpPostedFile file = context.Request.Files["file"];
 
                    // 验证文件
                    if (file.ContentLength > 0)
                    {
                        // 检查文件大小是否超过5MB
                        if (file.ContentLength > 5 * 1024 * 1024) // 5MB
                        {
                            throw new Exception("文件不能大于5M");
                        }
                        // 设置保存路径
                        string fileName = DateTime.Now.ToString("HHmmssfff")+ Path.GetFileName(file.FileName);
                        string riqi = DateTime.Now.ToString("yyyyMMdd");
                        string fileDirectory = "/UpFile/UpFile/" + riqi + "/";
                        if ((fileDirectory + fileName).Length>400) // 5MB
                        {
                            throw new Exception("文件名字过长");
                        }
                        string fileExtension = Path.GetExtension(fileName);
                        string savePath = context.Server.MapPath("~"+ fileDirectory) + fileName;
 
                        // 确保目录存在
                        if (!Directory.Exists(context.Server.MapPath("~"+ fileDirectory)))
                        {
                            Directory.CreateDirectory(context.Server.MapPath("~"+ fileDirectory));
                        }
 
                        // 保存文件
                        file.SaveAs(savePath);
 
                        // 返回JSON响应
                        var response = new
                        {
                            code = 1,
                            message = "文件上传成功",
                            fileName = fileName,
                            fileSize = file.ContentLength,
                            data = fileDirectory + fileName
                        };
 
                        context.Response.Write(new JavaScriptSerializer().Serialize(response));
                    }
                    else
                    {
                        throw new Exception("文件内容为空");
                    }
                }
                else
                {
                    throw new Exception("没有接收到文件");
                }
            }
            catch (Exception ex)
            {
                var errorResponse = new
                {
                    code = 2,
                    message = ex.Message
                };
 
                context.Response.Write(new JavaScriptSerializer().Serialize(errorResponse));
            }
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}