/    Sign up×
Community /Pin to ProfileBookmark

Trying to check the file size from an Input File object before upload it

I have a form with three Input File form objects in which the user can add image files. But I am worried about the problems these controls can lead, because to check the file size the server needs to receive the entire file, so serious problems may happen, for example:

  • 1.

    Malicious user trying to collapse the website sending big files.

  • 2.

    Users that have not seen the warning “Maximum size: 100 KB” for each image, and try to send three image files consisting in 1,5 MB each one. These users will have a bad experience waiting one hour or more to upload the images, and after that receive an alert saying “Oooops! Your image files are too big.” And also, web server will waste an important amount of resources. The same case with 100 users at the same time can be a nightmare!

  • I have been searching a way to check the file size on client side without having to force user to install anything, but there isn’ t nothing to do with javascript. But I found an interesting PHP article that explains a way to do it. The technique consists in:

    “A hidden field (measured in bytes) that precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.”

    The complete article is here:
    [url]http://us2.php.net/manual/en/features.file-upload.php[/url]

    But it doesn’ t explain details about how implementing exactly this solution.

    Does anyone know if a similar technique is it possible with ASP.NET? Other suggestions will be welcomed as well.

    Thank you

    to post a comment
    PHP

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @ShrineDesignsDec 20.2004 — use a .htaccess file and put this in it:php_value upload_max_filesize "100K"
    Copy linkTweet thisAlerts:
    @erick30authorDec 21.2004 — I use ASP.NET

    I opened a blank document, I wrote [I]php_value upload_max_filesize "100K"[/I], and I saved as 'max_size.htaccess' into the web folder. But it does the same.

    This is all I have to do?

    The html page:
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;

    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;new document&lt;/title&gt;

    &lt;/head&gt;
    &lt;body&gt;

    &lt;form name="form_1" action="http://www.yahoo.es" enctype="multipart/form-data" method="post"&gt;

    <i> </i>&lt;input type="hidden" name="MAX_FILE_SIZE" value="3000" /&gt;
    <i> </i>&lt;input type="file" name="attached_file" /&gt;
    <i> </i>&lt;input type="submit" value="Click here!!" /&gt;

    &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;


    The 'max_size.htaccess' file content:

    php_value upload_max_filesize "100K"
    Copy linkTweet thisAlerts:
    @erick30authorDec 21.2004 — Does anyone know how this feature works internally in PHP?: (more or less)

    "A hidden field (measured in bytes) that precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed."

    The complete article is here:

    http://us2.php.net/manual/en/features.file-upload.php

    I only want to adapt this feature (the advisory to the browser) to my ASP.NET pages.

    Thank you
    Copy linkTweet thisAlerts:
    @ShrineDesignsDec 22.2004 — the file must be ".htaccess"
    ×

    Success!

    Help @erick30 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...