ASP.NET: checking file size (width and height) before downloading

Is there a way to check the image size (height and width) before uploading it to the server?

I think that uses Javascript, but I do not know how to do this. Or maybe with some asp.net validator for the client.

Any tips?

+3
source share
5 answers

This cannot be done using javascript on the client side. You may find a file upload component written in flash memory, silver, or the like, that allows you to limit the downloaded files to type, size, and size.

+3
source

You simply cannot know the file size in JS on the client.

, , , :

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);
                  }
        }

,

EDIT: , . , FILE SIZE,

+2

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);
}
+1

GD , . ASP.net

0

You need to ask yourself, do you really think this is possible at all?

How will javascript open the file? How will he read? Do you have a library that can read any number of image formats?

0
source

Source: https://habr.com/ru/post/1720268/


All Articles