ASP.NET:在上载之前validation文件大小(宽度和高度)
有没有办法在上传到服务器之前validation图像大小(高度和宽度)?
我认为是使用Javascript但我不知道如何。 或者可能使用一些客户端asp.netvalidation器。
任何建议?
这不能在客户端使用javascript完成。 也许你会找到一个用flash,silverlight或类似文件编写的文件上传组件,它允许按类型,大小和尺寸限制上传的文件。
您根本无法在客户端上知道JS中的文件大小。
您可以做的是在请求到达服务器后检查文件大小,如果预期文件大小超过某个限制,则取消传输:
在您的上传模块的BeginRequest中:
HttpWorkerRequest workerRequest = (HttpWorkerRequest)context.GetType().GetProperty("WorkerRequest", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(context, null); // Indicates if the worker request has a body if (workerRequest.HasEntityBody()) { // Get the byte size of the form post. long contentLength = long.Parse((workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength))); if (contentLength > MAX_UPLOAD_FILE_SIZE * 1024 ) { workerRequest.CloseConnection(); context.Response.Redirect(SomeErrorPage); } }
还没有测试过,但理论上它可能会起作用
编辑:没关系,我是个白痴。 我认为他的意思是检查文件大小,而不是图像大小
我找到了这个Javascript片段并在我的系统上工作(IE 7.0.6001.18000)。 您应该知道并检查可能的跨浏览器问题。
var tempImage; function showDimensions() { var imageName = document.forms[0].elements['myFile'].value; if (imageName != '') { imageName = 'file:///' + escape(imageName.split('\\').join('/')); imageName = imageName.split('%3A').join(':'); tempImage = new Image(); tempImage.onload = getDimensions; tempImage.src = imageName + '?randParam=' + new Date().getTime(); // append a timestamp to avoid caching issues - which happen if you // overwrite any image with one of different dimensions, and try to // get the dimensions again even with cache settings to max, // in both ff and ie! } } function getDimensions() { alert(tempImage.width + ' x ' + tempImage.height); }
您可以使用GD来检查大小,看看这个项目是否为ASP.net包装器
你需要问自己,你真的认为这是可能的吗?
Javascript如何打开文件? 怎么会读它呢? 你有一个可以读取任意数量图像格式的库吗?
- ASP.NET Web应用程序 – 在部署时,System.Speech.dll对象未设置为对象的实例
- 将Base64图像发布到Mvc控制器
- 隐藏BoundField但仍能用C#获取值
- 是否可以使用Automapper将多个DTO对象映射到单个ViewModel?
- 默认情况下发送BASIC auth,而不是等待HTTP 401
- asp:基于条件的RequiredFieldValidatorvalidation
- NetworkCredential UseDefaultCredentials无法正常工作
- 无法在生产ASP.net网站上加载文件或程序集Microsoft.SqlServer.SqlClrProvider,它在哪里?
- 将数字转换为时间?