18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to return the size of a file

anoop.philipanoop.philip Members
edited November -1 in Sahi - Open Source
Is there any way to return the file size (in KB or MB or even bytes) of a file stored in a local drive?

Regards,
Anoop

Comments

  • Hi anoop,

    Since Sahi is based upon javascript, there is no direct method to return the file size of a file stored in a local drive. Usually the file size validation in done on the server side.

    You can go through the following link,

    http://4umi.com/web/javascript/fileread.php

    This might help you.

    If you require to perform the validation of file size during file uploading, You can test with
    var $fi =_file("fileId");
    _alert($fi.files[0].size);
    

    in Evaluate Expression textbox and It will alert with the size of file in bytes.

    This does'nt work during playback, since file upload validation is done with some sort of workaround.

    Regards,
    Theeran.
  • it shows error at playback with the code
    var $fi =_file("C:\\sahi\\userdata\\scripts\\test3mb.pdf");
    var $fs = $fi.files[0].size;
    _setValue(_textbox("username"), $fs);
    
    I tried to input the value into a text field to ensure whether it returns the file size.
    the log file shows error as showed me error

    _setValue(_textbox("username"), _file("C:\\sahi\\userdata\\scripts\\test3mb.pdf").files[0].size);
    TypeError: _sahi._file("C:\\sahi\\userdata\\scripts\\test3mb.pdf.pdf") is undefined

    Actually i want to validate the alert message if file size is above 2MB as it doesn't allow upload above 2MB. For that i need to get the file size and compare using if statement.

    Is there any solution?

    Regards,
    Anoop
  • Can anyone help with this?

    Regards,
    Anoop
  • Hi Anoop,

    Try with the following script,
    _setFile(_file("fileId"),"Path to file");
    // Change the "type" attribute of file field
        if (_isIE()){
            _call(_file("file").outerHTML = _file("file").outerHTML.replace(/type=file/, "type=text"));
        }else{
            _call(_file("file").type = "text");
        }
            // Set the value into the textbox
        _setValue(_textbox("fileId"), "Path to file");
        _set($fileName,_getText(_textbox("fileId")));
        var $file=_readFile($fileName);
        _alert("File size in Bytes:"+$file.length+" Bytes");
    

    This script will alert with the size of file being uploaded in bytes.

    Regards,
    Theeran.
  • narayannarayan Administrators
    In your Sahi script you can do this to get the file size:

    var $fileSize = new java.io.File("C:\\sahi\\userdata\\scripts\\test3mb.pdf").length();

    and then use $fileSize.

    Having said that, since you are the person who has created that static file, you can as well hardcode the size of the file in your testcase.

    Regards,
    Narayan
  • Thanks Narayan, Theeran,
    Both solutions work great! (with some difference between the number of bytes returned by the two solutions.)
    The issue is resolved now. thank you.
This discussion has been closed.