各位好,用JS脚本可以实现限制上传图片大小的功能,
我想请教各位如何用.cs后台代码实现限制上传图片大小的功能?关于上传,我的脚本如下:
前台:
 <input id="UpPhoto" type="file" name="UpPhoto" runat="server" />
后台:
HttpPostedFile upPhoto = UpPhoto.PostedFile;  
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);基于此段代码,在.cs中如何实现限制大小功能?
明天上班就要用了,麻烦各位啦~~

解决方案 »

  1.   

     if (!string.IsNullOrEmpty(this.imageFile.FileName.Trim()))
                        {
                            bool IsOk = Utility.CheckUpLoadImage(this.imageFile.FileName, this.imageFile.PostedFile.ContentLength, out extString);
                            if (IsOk && !string.IsNullOrEmpty(extString))
                            {
                                imagUrl = @"~/Images/InfoImages/" + infoId + "." + extString;
                                this.imageFile.SaveAs(Server.MapPath(imagUrl));
                            }
                        }
    public class Utility
    {
     public static bool CheckUpLoadImage(string postFileName, int postFileSize, out string extString)
            {
                extString = string.Empty;
                int sizetype = 1;
                string[] imgtype = {"jpg","gif","png"};
                if (postFileSize <= sizetype * 1000 * 1024)
                {
                    int posInt = postFileName.LastIndexOf('.');
                    if (posInt > -1)
                    {
                        extString = postFileName.Substring(posInt + 1);//扩展名
                        bool allowext = false;
                        foreach (string t in imgtype)
                        {
                            if (t.ToLower() == extString.ToLower())
                            {
                                allowext = true;
                                break;
                            }
                        }
                        if (!allowext)
                        {
                            extString = string.Empty;
                        }
                        return allowext;
                    }
                    throw new Exception("图片类型不正确!");            }
                else
                {
                    throw new Exception("图片大小超过限制!");
                }
            }
    }
      

  2.   

    请问图片上传用的是什么控件?
    CheckUpLoadImage()方法是哪里的?
      

  3.   

    asp.net的上传控件,ID是:imageFile
    没看到public class Utility
    {
    public static bool CheckUpLoadImage
    这个吗?
      

  4.   

    <httpRuntime maxRequestLength="51200" executionTimeout="600"/>
    Request.Files[0].ContentLength 
    FileUpload1.PostedFile.ContentLength var fso = new ActiveXObject("Scripting.FileSystemObject");
             var file = fso.GetFile(obj.value);
            if(file.Size>1024*1024*4)
            {}