如何使用ajaxfileupload asp.net上传图像宽度和高度

时间:2012-12-03 04:18:39

标签: c# asp.net

我认为ajaxcontroltoolkit:ajaxfileupload有太多bug并且已经减少了。因为我在我的项目中经常使用这个组件,所以我克服了许多严重令人不安的错误和这个控件的功能。 现在,我真的需要使用ajaxfileupload上传图像的宽度和高度,然后保存它以检查宽度或高度是否正确并根据检索到的信息,在图像宽度和高度不兼容的情况下通知用户然后阻止进程进一步保存图片。 请问任何想法?!

HTML方面:

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
        onuploadcomplete="AjaxFileUpload1_UploadComplete" ThrobberID="myThrobber" MaximumNumberOfFiles="1" AllowedFileTypes="jpg,jpeg"/>

代码背后:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    string filePath = "~/upload/" + e.FileName;
    AjaxFileUpload1.SaveAs(filePath);

}

1 个答案:

答案 0 :(得分:6)

AjaxFileUpload只处理上传,但没有提供有关文件本身的任何信息。你需要使用像

这样的东西
System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath(filePath));
width = objImage.Width;
height = objImage.Height; 

如果您不想将文件加载到内存中,可以查看How do I reliably get an image dimensions in .NET without loading the image?Getting image dimensions without reading the entire file

另一种方法是使用Plupload或类似的工具检查客户端的图像宽度和高度。