Click to See Complete Forum and Search --> : Java Script and getting local file's size


ritug
11-21-2003, 02:57 AM
I want to identify a local file's path using javascript that i am trying to upload using ASP.can any body suggest me the way by which i can get the file size o a local file using it's name.:rolleyes:

Pittimann
11-21-2003, 03:14 AM
Hi!

Using ActiveX you can get the size of a local file like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<SCRIPT type="text/javascript">
function FileCheck(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fileObj = fso.GetFile("C:\\PathToFile\\FileName");
SizeOfFile = fileObj.Size;//actual Filesize in Bytes
//(code after this is for calculating size in kilobytes)...
SizeOfFile = SizeOfFile/1024
SizeOfFile = SizeOfFile.toString().replace(/\$|\,/g,'');
if(isNaN(SizeOfFile))
num = "0";
sign = (SizeOfFile == (SizeOfFile = Math.abs(SizeOfFile)));
SizeOfFile = Math.floor(SizeOfFile*100+0.50000000001);
AfterDot = SizeOfFile%100;
SizeOfFile = Math.floor(SizeOfFile/100).toString();
if(AfterDot<10)
AfterDot = "0" + AfterDot;
for (var i = 0; i < Math.floor((SizeOfFile.length-(1+i))/3); i++)
SizeOfFile = SizeOfFile.substring(0,SizeOfFile.length-(4*i+3))+','+
SizeOfFile.substring(SizeOfFile.length-(4*i+3));
SizeOfFile= (((sign)?'':'-') + SizeOfFile + ',' + AfterDot) + ' kB';
alert(SizeOfFile);
}
</SCRIPT>
<center>
<input type = button onclick="FileCheck()" value="Check filesize">
</center>
</body>
</html>

Please note that the separator for the decimals is a comma instead of a dot (it's just like that in Germany)...

Cheers - Pit

ritug
11-21-2003, 04:52 AM
I think if i will use this code there will be security problems arising because we cannot modify java script settings on client side client . and may be we are not allowed to access these details because of that.
can u help me regarding this.

Pittimann
11-21-2003, 05:06 AM
Hi!

Do you want to upload files from someone else's machine or your own?? If it is from your own - no problem. If it is from a visitor's machine you will fail because the user has to use the browse-button of the input (type=file) himself - just like you will have to do for the upload.

If you get a security alert when running the script (pointing out, that there might be a problem because of some ActiveX element) just click "Ok" and the script will tell you the filesize in an alert...

Cheers - Pit

ritug
11-21-2003, 05:27 AM
I am creating a site in which user has to upload a file by clicking on type file and choosing a file from his own system.
so if the java script settings on the client's machine restrict me from accessing the details of file i wouldn't be able to access it.

Pittimann
11-21-2003, 06:00 AM
Hi!

I never did any asp-stuff, so I don't know what the requirements are.

I manage file uploads with php. The user just has to select the file to be uploaded in the form (input type = file) click the submit-button and the upload starts. No need for knowing the filesize beforehand (and - as far as I know - no other way than I posted before to get the filesize). The original path, file name, size and other stuff are included in the information provided by the input's value.

Does your webspace support php??

Cheers - Pit...

ray326
11-21-2003, 09:42 AM
Originally posted by ritug
I want to identify a local file's path using javascript that i am trying to upload using ASP.can any body suggest me the way by which i can get the file size o a local file using it's name.:rolleyes:
You can't get local file information using Javascript running in a browser because that is a HUGE security hole. Use a form-based upload strategy and deal with the file size on the server side.