Skip to content
Advertisement

getting file size in javascript

given a path of a file like:
C:file.jpg
how can i get the size of the file in javascript?

Advertisement

Answer

If it’s not a local application powered by JavaScript with full access permissions, you can’t get the size of any file just from the path name. Web pages running javascript do not have access to the local filesystem for security reasons.

HTML5 has the File API. If a user selects the file for an input[type=file] element, you can get details about the file from the files collection:

<input type=file id=fileSelector>
fileSelector.onchange = () => {
    alert(fileSelector.files[0].size);
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement