文件的上传和控制

这是在detailsview控件里的一个文件上传功能,照着msdn修改的,加上了控制大小的功能。感觉foreach里的很多代码都应该拿外边去,但是还没想到好方法。
还没测试,不知道能否上传成功~
 1 foreach  (DetailsViewRow row  in  DetailsView1.Rows)
 2                  {
 3                    FileUpload fl = (FileUpload)FindControl("FileUpload1");
 4                    bool fileOK = false;
 5                    String path = Server.MapPath("~/aa/");
 6                    if (fl.HasFile)
 7                    {
 8                        String fileExtension =Path.GetExtension(fl.FileName).ToLower();
 9                        String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
10                        
11                        for (int i = 0; i < allowedExtensions.Length; i++)
12                        {
13                            if (fileExtension == allowedExtensions[i]&&h.MaxRequestLength<=1024)
14                            {
15                                fileOK = true;
16                            }

17                        }

18                    }

19                    Label lable = (Label)FindControl("lable1");
20                    if (fileOK)
21                    {
22                        try
23                        {
24                            fl.PostedFile.SaveAs(path + fl.FileName);
25                            lable.Text = "上传成功!";
26                        }

27                        catch (Exception ex)
28                        {
29                            lable.Text = "上传失败!";
30                        }

31                    }

32                    else
33                    {
34                        lable.Text = "文件类型不符或文件过大(1024K以内)!";
35                    }

36                   
37                }
              

你可能感兴趣的:(文件)