/    Sign up×
Community /Pin to ProfileBookmark

How to check the size of a file

Hello all,

I let the users to upload files and not only images.
I want before the file upload to check the size of the file.

I now how to do that with images but not with files.

Does any one have an idea?

Thanks

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoOct 14.2005 — It isn't really possible. The best thing to do is to set a file limit or to upload the file to a temp folder and then have a server side script that checks the size, and if it is to big then delete it and alert the user.
Copy linkTweet thisAlerts:
@MjhLkwdOct 14.2005 — IE only:

[CODE]<HTML>
<Head>
<Script type="text/javascript">

function getStats(fName){

var fullName = fName;
var shortName = fullName.match(/[^/\]+$/);
document.Form1.dispSize.value = fileSize(fullName);
document.Form1.dispName.value = shortName;
}

</Script>
<Script type="text/vbscript">

Function fileSize(fileSpec)

Set fso = CreateObject("Scripting.FileSystemObject")
Set contentFile = fso.GetFile(fileSpec)
isSize = contentFile.Size
fileSize = isSize
Set contentFile = Nothing
Set fso = Nothing

End Function

</Script>
</Head>
<Body>
<Form name='Form1'>
Choose a file: <input type='file' onchange="getStats(this.value)"><br>
File Name: <input name='dispName' size='20'><br>
File Size: <input name='dispSize' size='10' readonly><br>
<input type='reset'><br>
<input type='submit' value="Submit">
</Form>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@noamwayauthorOct 14.2005 — the explorer alert he want ActivX.

You may be another idea?

I don't want the user upload a 10MB file and then tell him it's not ok.
Copy linkTweet thisAlerts:
@MjhLkwdOct 14.2005 — I wrote "IE only" at the top of my response.

The code is what it is.

JavaScript alone isn't capable of returning the size of a file/document.

You could place a text "notice" in your page, informing users beforehand that ActiveX will check the size of the file.

You know, sometimes half of a solution is better than nothing.
Copy linkTweet thisAlerts:
@noamwayauthorOct 14.2005 — Thanks Mike!!!
×

Success!

Help @noamway 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.29,
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,
)...