C# 文件上传后台处理

 1  protected void Page_Load(object sender, EventArgs e)
 2         {
 3             
 4             string BelongedTo = Request["BelongedTo"].ToString();
 5             string RecordNumber = Request["RecordNumber"].ToString();
 6             string ApplyNum = Request["ApplyNum"].ToString();
 7             string PhotoType = Request["PhotoType"].ToString();
 8             string UploadTerminal = Request["UploadTerminal"].ToString();
 9             try
10             {
11                 HttpFileCollection files = HttpContext.Current.Request.Files;
12                 if (files == null) return;
13                 for (int i = 0; i < files.Count; i++)
14                 {
15                     HttpPostedFile item = files[i];
16                     if (item.ContentLength > 1024 * 1024)
17                     {
18                         Response.Write("{'failure':'图片大小不能超过1M'}");
19                         return;
20                     }
21                     else if (item.ContentLength < 50 * 1024)
22                     {
23                         Response.Write("{'failure':'图片大小不能小于50KB'}");
24                         return;
25                     }
26                     string fileOldName = item.FileName;
27                     string fileSuffixes = fileOldName.Substring(fileOldName.LastIndexOf(".") + 1).Trim();
28                     string fileNewName = DateTime.Now.Ticks.ToString() + new Random().Next(1000, 9999).ToString() + "." + fileSuffixes;
29                     item.SaveAs(HttpContext.Current.Request.MapPath("../../../../upload/")+ fileNewName);
30                     
31                     ProjectOverviewDAL.AddCrateTargetImg(new ProTargetUpdatePhotoEntity {
32                         BelongedTo = BelongedTo,
33                         RecordNumber=RecordNumber,
34                         ApplyNum = ApplyNum,
35                         PhotoType = PhotoType,
36                         UploadTerminal = UploadTerminal,
37                         PhotoUrl="../../../../upload/"+ fileNewName
38                     });
39                 }
40             }
41             catch (Exception ex)
42             {
43                 Response.Write("{'success':'false', 'files':'文件上传失败: " + ex.Message + "'}");
44                 Response.End();
45             }
46 
47             Response.Write("{'success':'true'}");
48             Response.End();
49         }

 

转载于:https://www.cnblogs.com/gavin0517/p/5144224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值