开发者

getting file size in javascript

开发者 https://www.devze.com 2023-01-01 21:48 出处:网络
given a path of a file like: C:\\file.jpg how can i get the size of th开发者_开发问答e file in javascript?If it\'s not a local application powered by JavaScript with full access permissions, you can\'

given a path of a file like:

C:\file.jpg

how can i get the size of th开发者_开发问答e file in javascript?


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);
}


function findSize() {
    var fileInput =  document.getElementById("fUpload");
    try{
        alert(fileInput.files[0].size); // Size returned in bytes.
    }catch(e){
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var e = objFSO.getFile( fileInput.value);
        var fileSize = e.size;
        alert(fileSize);    
    }
}


You could probably try this to get file sizes in kB and MB Until the file size in bytes would be upto 7 digits, the outcome would be in kbs. 7 seems to be the magic number here. After which, if the bytes would have 7 to 10 digits, we would have to divide it by 10**3(n) where n is the appending action . This pattern would repeat for every 3 digits added.

let fileSize = myInp.files[0].size.toString();

if(fileSize.length < 7) return `${Math.round(+fileSize/1024).toFixed(2)}kb`
    return `${(Math.round(+fileSize/1024)/1000).toFixed(2)}MB`


If you could access the file system of a user with javascript, image the bad that could happen.

However, you can use File System Object but this will work only in IE:

http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript


Try this one.

function showFileSize() {
  let file = document.getElementById("file").files[0];
  if(file) {
    alert(file.size + " in bytes"); 
  } else { 
    alert("select a file... duh"); 
  }
}
<input type="file" id="file"/>
<button onclick="showFileSize()">show file size</button>


It's 2022 and of course this is possible by using fetch to get the file and returning a blob, then reading the size property of the returned blob. However this works if you run a local server, as it's not possible to access the local file system with Javascript (see the Note below).

function getFileStats(url){
  let fileBlob;
  fetch(url).then((res) => {
    fileBlob = res.blob();
    return fileBlob;
  }).then((fileBlob)=>{
    // do something with the result here
    console.log([fileBlob.size, fileBlob.type]);
  });
}

// Example of usage
getFileStats('http://127.0.0.1:5500/img/mobileset/1.jpg');

// You should get something like this in the console
[195142, 'image/jpeg']

Both the size and type properties have good browser coverage currently. Size is expressed in bytes, so roll your own formula to convert. This is just a quick example. It can be written better and adapted to your case. Processing a large number of files as blobs in the browser is likely to be a very compute-intensive task, especially for low-end computers, so use this accordingly.

Note: Javascript could never do this for a file on the local computer (at path C:\file.jpg) like in your example, because it's not supposed to do this by design. It would be a major security risk to allow web pages direct access to the local file system, it's only done through two filters: how the OS allows it through its API and how the browser allows it knowing that it needs to never expose the local file system to the internet. So my example will work if you run a server locally and can fetch the file through the server. Or if you fetch it from a remote server, if it allows.


You can't get the file size of local files with javascript in a standard way using a web browser.

But if the file is accessible from a remote path, you might be able to send a HEAD request using Javascript, and read the Content-length header, depending on the webserver


You cannot.

JavaScript cannot access files on the local computer for security reasons, even to check their size.

The only thing you can do is use JavaScript to submit the form with the file field to a server-side script, which can then measure its size and return it.


You cannot as there is no file input/output in Javascript. See here for a similar question posted.


To get the file size of pages on the web I built a javascript bookmarklet to do the trick. It alerts the size in kb's. Change the alert to a prompt if you want to copy the filesize.

Here's the bookmarklet code for the alert.

<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);alert(c+' kb');">Doc Size</a>

Here's the bookmarklet code for the prompt.

<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);prompt('Page Size',c+' kb');">Doc Size</a>

See it in action at http://bookmarklets.to.g0.to/filesize.php

0

精彩评论

暂无评论...
验证码 换一张
取 消