“参数无效”在asp.net mvc中将HttpPostedFileBase转换为Image时抛出的异常 [英] "Parameter is not valid" exception thrown when convert HttpPostedFileBase to Image in asp.net mvc

查看:143
本文介绍了“参数无效”在asp.net mvc中将HttpPostedFileBase转换为Image时抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public ActionResult UploadImageWithRetry()

{

List< httppostedfilebase> errorfiles =(Session [errorList])如List< httppostedfilebase> ;;

if(errorfiles!= null)

{

foreach(错误文件中的HttpPostedFileBase文件)

{



if(CheckImageType(file))

{

if(CheckImageSize(file))//从这里打电话

{



}

}

}

}

}

}





public ActionResult UploadImageWithRetry()
{
List<httppostedfilebase> errorfiles = (Session["errorList"]) as List<httppostedfilebase>;
if (errorfiles != null)
{
foreach (HttpPostedFileBase file in errorfiles)
{

if (CheckImageType(file))
{
if (CheckImageSize(file))//call from here
{

}
}
}
}
}
}


public bool CheckImageSize(HttpPostedFileBase aFile)
        {
            bool isValid = false;
            if (aFile.ContentLength > 0)
            {
                try
                {
                    System.Drawing.Image imageObj = System.Drawing.Image.FromStream(aFile.InputStream); //error generated here
                    if ((imageObj.Width == 640 && imageObj.Height == 960) || (imageObj.Width == 640 && imageObj.Height == 1136) || (imageObj.Width == 768 && imageObj.Height == 1024) || (imageObj.Width == 1536 && imageObj.Height == 2048))
                    {
                        isValid = true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return true;
        }

推荐答案

参数无效是来自GDI的一般错误消息,基本上意味着那不是'我认识到的图像



最明显的原因是,您尝试上传的文件不是有效的图像文件,或者是损坏的或不完整的文件。 />
尝试将流数据保存在临时文件中,并手动检查它以查看流包含的数据类型。
"Parameter Not Valid" is a generic error message from the GDI, which basically means "that isn't an image I recognise"

The most obvious reason is that whatever file you are trying to upload is not a valid image file, or is corrupt or incomplete.
Try saving the stream data in a temporary file, and examine it manually to see what kind of data the stream contains.


这篇关于“参数无效”在asp.net mvc中将HttpPostedFileBase转换为Image时抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆