当前位置: 首页>>代码示例>>C#>>正文


C# HttpPostedFile.IsImage方法代码示例

本文整理汇总了C#中System.Web.HttpPostedFile.IsImage方法的典型用法代码示例。如果您正苦于以下问题:C# HttpPostedFile.IsImage方法的具体用法?C# HttpPostedFile.IsImage怎么用?C# HttpPostedFile.IsImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.HttpPostedFile的用法示例。


在下文中一共展示了HttpPostedFile.IsImage方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateUploadedFile

        /// <summary>
        /// Valiate the upload file is allowed to be save
        /// </summary>
        /// <param name="context">The HTTP Context</param>
        /// <param name="postedFile">The uploaded file</param>
        /// <param name="fileName">The destination file name</param>
        /// <param name="fileType">The destination file MIME type</param>
        /// <param name="fileSize">The file full size (in multipart upload it can be that the postedFile is not in the same size)</param>
        /// <param name="errorMessage">An error message related to the vertification process</param>
        /// <returns>Boolean value that indicates if we can save the file</returns>
        public virtual bool ValidateUploadedFile(HttpContext context, HttpPostedFile postedFile, string fileName, string fileType, long fileSize, out string errorMessage)
        {
            errorMessage = string.Empty;

            //-------------------------------------------
            //  Do we exceed the IIS post size?
            //-------------------------------------------
            if (fileSize > this.GetIISMaxAllowedPostSize() * 1024)
            {
                errorMessage = GetResourceString(context, "PostMaxSizeExceeded");
                return false;
            }

            //-------------------------------------------
            //  Is our file matching the allowed files pattern?
            //-------------------------------------------
            if (!this.AcceptedFilesPattern.IsMatch(fileName))
            {
                errorMessage = GetResourceString(context, "InvalidFileType");
                return false;
            }

            //-------------------------------------------
            //  Check file size
            //-------------------------------------------
            if (this.FileMaxSize > 0
                && fileSize > this.FileMaxSize)
            {
                errorMessage = GetResourceString(context, "FileMaxSizeExceeded", ((long)this.FileMaxSize).ToFileSize());
                return false;
            }

            if (this.FileMinSize > 0
                && fileSize < this.FileMinSize)
            {
                errorMessage = GetResourceString(context, "FileMinSizeNotReached", ((long)this.FileMinSize).ToFileSize());
                return false;
            }

            //-------------------------------------------
            //  We're exceeding the maximum number of files?
            //-------------------------------------------
            if (this.MaximumNumberOfFiles > -1
                && this.CountAvailableFiles(context) > this.MaximumNumberOfFiles)
            {
                errorMessage = GetResourceString(context, "MaximumNumberOfFilesExcceded", this.MaximumNumberOfFiles.ToString());
                return false;
            }

            //-------------------------------------------
            //  We DO got an image, don't we?
            //-------------------------------------------
            if (!postedFile.IsImage())
            {
                errorMessage = "NotValidImage";
                return false;
            }

            //-------------------------------------------
            //  Image dims vertification
            //-------------------------------------------
            if (this.ImageMinAllowedWidth > -1
                || this.ImageMinAllowedHeight > -1
                || this.ImageMaxAllowedWidth > -1
                || this.ImageMaxAllowedHeight > -1)
            {
                /* Note: we're not using try-catch block
                 * because we do know that we can instantiate Bitmap from the input stream
                 * since we've already done it in IsImage() extension method. */

                using (Bitmap image = new Bitmap(postedFile.InputStream))
                {
                    if (this.ImageMinAllowedWidth > image.Width)
                    {
                        errorMessage = GetResourceString(context, "ImageMinAllowedWidthNotMet", this.ImageMinAllowedWidth.ToString());
                        return false;
                    }

                    if (this.ImageMinAllowedHeight > image.Height)
                    {
                        errorMessage = GetResourceString(context, "ImageMinAllowedHeightNotMet", this.ImageMinAllowedHeight.ToString());
                        return false;
                    }

                    if (this.ImageMaxAllowedWidth < image.Width)
                    {
                        errorMessage = GetResourceString(context, "ImageMaxAllowedWidthExceeded", this.ImageMaxAllowedWidth.ToString());
                        return false;
                    }

//.........这里部分代码省略.........
开发者ID:bUKaneer,项目名称:jQuery-File-Upload,代码行数:101,代码来源:DynamicPicturesUploaderHandler.cs


注:本文中的System.Web.HttpPostedFile.IsImage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。