protected void btnUpload_Click(object sender, EventArgs e)
{
int width = 0, height = 0;
if (fileUpload.HasFile)
{
string dict = "/Content/Files/" + DateTime.Now.ToShortDateString() + "/";
string serverPath = Server.MapPath(dict);
if (!System.IO.File.Exists(serverPath))
{
System.IO.Directory.CreateDirectory(serverPath);
string filePath = serverPath + fileUpload.FileName;
fileUpload.SaveAs(filePath);
getImageSize(filePath, out width, out height);
if (width >= 400 || height >= 400)
{
literFile.Text = "上传文件尺寸必须是:400*400";
System.IO.File.Delete(filePath);
}
else
{
literFile.Text = width + ":" + height;
}
}
}
else
{
Response.Write("没有上传文件");
}
}
private void getImageSize(string filePath, out int iWidth, out int iHeight)
{
iWidth = iHeight = 0;
//FileStream
System.Drawing.Image imgFile = System.Drawing.Image.FromFile(filePath);
if (imgFile != null)
{
iWidth = imgFile.Width;
iHeight = imgFile.Height;
}
imgFile.Dispose();
}