/    Sign up×
Community /Pin to ProfileBookmark

Detecting file size using JavaScript

I see this has been asked but not answered 2 years ago. I have scoured the web and have realized that this can be done. My client is focused solely on Firefox and IE. I have gathered code to retrieve the size for both. My only concern is, for IE, I have to use ActiveX. I have noticed that later versions of IE allow the user to filter out ActiveX objects.

So, my question is, given that getting the file size of a file to be uploaded can be done (at present), what are your opinions that it will still work in future versions of browsers?

Thanks,

Tim

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 28.2011 — As you said, you have used ActiveX, not JavaScript. As far as I know JavaScript, as a client-side language, can not measure the size of a server-file. You should use a server-side language for that, such as PHP, Java, Pearl, ASP.NET, etc.

On the other hand, ActiveX is a language specific to Microsoft and MS products. There were some rumors about the implementation of a sort-of ActiveX in Firefox, but I saw no reliable results on the issue.
Copy linkTweet thisAlerts:
@tpdietzauthorSep 28.2011 — Actually, I use the following JavaScript in Firefox which works perfectly:

document.getElementById('input_file_id').files[0].fileSize

ActiveX works fine as well. I'm just wondering if they will be available down the road.

We use struts and there is a max file upload size that is set and when someone tries to upload a file greater than that max, we get a server error. I have no access to that server-side code, unfortunately.
Copy linkTweet thisAlerts:
@rnd_meSep 28.2011 — activex is on it's way out, input.files is the new standard, but i don't know if IE supports it yet...

if the file is on the server, a simple HEAD request will provide the content-length, aka the filesize...
Copy linkTweet thisAlerts:
@tpdietzauthorSep 28.2011 — Thanks for the reply. I guess we'll update the code when we need to.

And, no, the file is not on the server, that's easy to get the size of. These are files on the client's machine that the client is trying to upload via the <input type="file"/> element.
×

Success!

Help @tpdietz 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.18,
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,
)...