use*_*672 4 c# asp.net asp.net-mvc asp.net-mvc-4
如何在ASP.NET MVC中获取上传图像的尺寸?
这是我FileHelper上传文件的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
namespace workflow.helpers
{
public class FileHelper
{
private HttpPostedFileBase file {get; set;}
public bool ValidFile { get; set;}
public FileHelper(HttpPostedFileBase File, string Extensions, int MaxSize=1, bool IsImage = true, string Dimensions="")
{
this.file = File;
validateFile(Extensions, MaxSize, IsImage, Dimensions);
}
public string uploadFile()
{
if (this.file.ContentLength > 0)
{
if (this.ValidFile)
{
var fileName = Path.GetFileName(this.file.FileName);
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/sharedfiles/uploads/images"), fileName);
this.file.SaveAs(path);
return this.file.FileName;
}
else return "invalid file";
}
return "";
}
private void validateFile(string extensions, int MaxSize, bool IsImage = true, string Dimensions )
{
if (extensions.Contains(Path.GetExtension(this.file.FileName).Replace(".", "")))
{
if ((((double)(file.ContentLength) / 1024) / 1024) < MaxSize)
this.ValidFile = true;
}
else
this.ValidFile = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我仍然不知道是否有可能检查此类中上传图像的尺寸.
Moh*_*mad 11
private string GetImageDimension()
{
System.IO.Stream stream = fu1.PostedFile.InputStream;
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
int height = image.Height;
int width = image.Width;
return "Height: " + height + "; Width: " + width;
}
Run Code Online (Sandbox Code Playgroud)
你可以得到它InputStream,它可以property在HttpPostedFileBase对象中获得,如果你写的话很容易就能得到它file.InputStream
| 归档时间: |
|
| 查看次数: |
7494 次 |
| 最近记录: |