public void Deny_Unrestricted()
 {
     Assert.IsNotNull(coll.AllKeys, "AllKeys");
     coll.CopyTo(new object[0], 0);
     Assert.IsNull(coll.Get("mono"), "Get(string)");
     Assert.IsNull(coll["mono"], "this[string]");
     try {
         Assert.IsNull(coll[0], "this[int]");
     }
     catch (ArgumentOutOfRangeException) {
         // normal (can't avoid it)
     }
     try {
         Assert.IsNull(coll.GetKey(0), "GetKey(int)");
     }
     catch (ArgumentOutOfRangeException) {
         // normal (can't avoid it)
     }
     try {
         Assert.IsNull(coll.Get(0), "Get(int)");
     }
     catch (ArgumentOutOfRangeException) {
         // normal (can't avoid it)
     }
 }
示例#2
0
        /// <summary>
        /// 多文件上传
        /// </summary>
        /// <param name="files">Request.Files 集合</param>
        /// <param name="controlName">上传控件的name 值</param>
        /// <param name="folderName">文件上传的文件夹名</param>
        /// <param name="filePath">返回文件的存储虚拟路径集合</param>
        /// <returns></returns>
        public static bool FileUpLoad(HttpFileCollection files, string controlName, string folderName, out IList <string> filePath)
        {
            filePath = new List <string>();
            if (files != null && files.Count > 0)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    if (files.GetKey(i).ToString().ToUpper() == controlName.ToUpper())
                    {
                        string path = "/uploadFiles/" + folderName + "/" + (GetTimeRandom() + Path.GetExtension(files[i].FileName));
                        filePath.Add(path);

                        try
                        {
                            files[i].SaveAs(System.Web.HttpContext.Current.Server.MapPath(path));
                        }
                        catch
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
示例#3
0
        private static HttpPostedFile[] GetUploadedFiles(HttpFileCollection files)
        {
            var validFiles = new List <HttpPostedFile>();

            for (var i = 0; i < files.Count; i++)
            {
                var key = files.GetKey(i);

                if (key != "upload[]")
                {
                    continue;
                }

                var file = files[i];

                if (string.IsNullOrWhiteSpace(file.FileName))
                {
                    continue;
                }

                validFiles.Add(file);
            }

            return(validFiles.ToArray());
        }
示例#4
0
        private void InitializeFiles()
        {
            if (IsDesignTime || IsFilesInitialized)
            {
                return;
            }
            // Initialize the Files property
            ArrayList fileArrayList = new ArrayList();

            if (UploadModule.IsEnabled)
            {
                UploadedFileCollection allFiles = UploadModule.Files;
                // Get only the files that were uploaded from this control
                for (int i = 0; allFiles != null && i < allFiles.Count; i++)
                {
                    if (allFiles.GetKey(i) == this.UniqueID)
                    {
                        UploadedFile uploadedFile = allFiles[i];
                        if (uploadedFile.IsUploaded)
                        {
                            fileArrayList.Add(allFiles[i]);
                        }
                    }
                }
            }
            else
            {
                HttpFileCollection allFiles = HttpContext.Current.Request.Files;
                // Get only the files that were uploaded from this control
                for (int i = 0; allFiles != null && i < allFiles.Count; i++)
                {
                    if (allFiles.GetKey(i) == this.UniqueID)
                    {
                        UploadedFile uploadedFile
                            = UploadModule.ConvertToUploadedFile(this.UniqueID, (HttpPostedFile)allFiles[i]);
                        if (uploadedFile == null)
                        {
                            continue;
                        }
                        if (uploadedFile.IsUploaded)
                        {
                            fileArrayList.Add(uploadedFile);
                        }
                        else
                        {
                            uploadedFile.Dispose();
                        }
                    }
                }
            }
            _files = new UploadedFile[fileArrayList.Count];
            Array.Copy(fileArrayList.ToArray(), _files, _files.Length);
            IsFilesInitialized = true;
        }
示例#5
0
    private void Page_Load(Object sender, EventArgs e)
    {
// <Snippet1>
        int loop1;
        HttpFileCollection MyFileColl = Request.Files;

        for (loop1 = 0; loop1 < MyFileColl.Count; loop1++)
        {
            if (MyFileColl.GetKey(loop1) == "CustInfo")
            {
                //...
            }
        }

// </Snippet1>
    }
示例#6
0
        /// <summary>
        ///  验证文件上传格式
        /// </summary>
        /// <param name="files"> files 集合 </param>
        /// <param name="controlName">控件name值</param>
        /// <param name="allowExtensions">上传文件格式 如[".jpg","gif"]</param>
        /// <param name="allowExtensions">文件上传大小限制:单位为M,为null则默认</param>
        /// <param name="allowExtensions">验证返回信息</param>
        /// <returns></returns>
        public static bool CheckFileType(HttpFileCollection files, string controlName, string[] allowExtensions, int?fileSize, out string msg)
        {
            bool fileAllow = false;

            msg = "";
            if (fileSize == null)
            {
                fileSize = 5;
            }
            ;
            if (files != null && files.Count > 0)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    if (files.GetKey(i).ToString() == controlName)
                    {
                        if (files[i] != null && files[i].FileName.Trim() == "")
                        {
                            return(true);
                        }

                        if (files[i].ContentLength / 1024 / 1024 > fileSize)
                        {
                            msg = files[i].FileName + " 文件过大,无法上传!";
                            return(false);
                        }
                        string fileExtension = System.IO.Path.GetExtension(files[i].FileName).ToLower();
                        for (int j = 0; j < allowExtensions.Length; j++)
                        {
                            if (fileExtension.ToLower() == allowExtensions[j].ToLower())
                            {
                                fileAllow = true;
                            }
                        }
                    }
                }
            }
            if (!fileAllow)
            {
                msg = "文件格式不正确!";
            }
            else
            {
                msg = "验证成功!";
            }
            return(fileAllow);
        }
示例#7
0
 public void doFormUploadDisk()
 {
     if (!this.Page.IsPostBack)
     {
         string             str   = base.Server.MapPath(this.Session["uploadpath"].ToString());
         HttpFileCollection files = base.Request.Files;
         for (int i = 0; i < files.Count; i++)
         {
             HttpPostedFile httpPostedFile = files[i];
             if (files.GetKey(i).ToUpper() == "EDITFILE")
             {
                 string text = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf('\\') + 1);
                 httpPostedFile.SaveAs(str + "\\" + text);
                 base.Response.Write("文件名称: " + text + "<br>");
                 base.Response.Write("大小: " + httpPostedFile.ContentLength.ToString() + "\t字节<br>");
                 base.Response.Write("已成功保存到Web服务器上。<br>");
             }
         }
     }
 }
示例#8
0
 public override string GetKey(int index)
 {
     return(_collection.GetKey(index));
 }