开发者

JavaScript filesize calculation

开发者 https://www.devze.com 2023-03-05 04:34 出处:网络
I want to calculate the file size of the file selected by userfrom file browser window. How ca开发者_开发知识库n I achieve it without using activexobject in JavaScript.You can use id for input and get

I want to calculate the file size of the file selected by user from file browser window. How ca开发者_开发知识库n I achieve it without using activexobject in JavaScript.


You can use id for input and get size when onChange:

<input type=file id=fileSelector>

fileSelector.onchange = () => {
    alert(fileSelector.files[0].size);
}

Another way, use can use API to get file size, like:

fileSelector.onchange = () => {
  let fileBlob;
  const formData = new FormData();
  formData.append('file', fileSelector.files[0]); 
  fetch(formData).then((res) => {
    fileBlob = res.blob();
    return fileBlob;
  }).then((fileBlob)=>{
    // file size: fileBlob.size
    console.log([fileBlob.size, fileBlob.type]);
  });
}

Hope that it helps

0

精彩评论

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