简体   繁体   English

ASP.NET:在上载之前验证文件大小(宽度和高度)

[英]ASP.NET: Validate file size (width and height) before upload

Is there some way to validate the image size (height and width) before upload it to server? 有没有办法在将图像大小(高度和宽度)上传到服务器之前验证它?

I think is using Javascript but I don't know how. 我认为是使用Javascript但我不知道如何。 Or maybe with some client asp.net validator. 或者可能使用一些客户端asp.net验证器。

Any advice? 有什么建议?

This can't be done using javascript on the client-side. 这不能在客户端使用javascript完成。 Maybe you'll find a file upload component written in flash, silverlight or similar that allows to restrict the files being uploaded by the type, size and dimensions. 也许你会发现用flash,silverlight或类似文件编写的文件上传组件允许按类型,大小和尺寸限制上传的文件。

You simply cannot know the file size in JS on the client. 您根本无法在客户端上知道JS中的文件大小。

What you could do is inspect the file size once the request arrives on the server, then cancel the transfer if the expected file size is over a certain limit: 可以做的是在请求到达服务器后检查文件大小,如果预期文件大小超过某个限制,则取消传输:

In your upload module's BeginRequest: 在您的上传模块的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);
                  }
        }

Haven't tested this yet, but theoretically it might work 还没有测试过,但理论上它可能会起作用

EDIT: never mind, i'm an idiot. 编辑:没关系,我是个白痴。 I thought he meant checking the FILE SIZE, not IMAGE SIZE 我认为他的意思是检查文件大小,而不是图像大小

I found this Javascript snippet and worked on my system (IE 7.0.6001.18000). 我找到了这个Javascript片段并在我的系统上工作(IE 7.0.6001.18000)。 You should be aware and check for possible cross browser problems. 您应该知道并检查可能的跨浏览器问题。

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包装器

You need to ask yourself, do you really think this is possible at all? 你需要问自己,你真的认为这是可能的吗?

How would Javascript open a file? Javascript如何打开文件? How would it read from it? 它怎么读? Do you have a library that can read any number of image formats? 你有一个可以读取任意数量图像格式的库吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM