admin管理员组文章数量:1389768
Trying to get the size of a file when the user uploads file. When user clicks on the browse button to upload file, but instead clicks the cancel button. The filesize type throws an error of undefined.
I tried to use typeof to test condition of filesize is undefined but it does not work. Need help to catch error.
$('#fileupload-image').change(function() {
$('#select_file').hide();
$('#upload_image_button').slideDown('fast');
var filename = $(this).val();
if(typeof this.files[0].size == 'undefined')
{
this.files[0].size = '';
}
else
{
var filesize = this.files[0].size;
}
Trying to get the size of a file when the user uploads file. When user clicks on the browse button to upload file, but instead clicks the cancel button. The filesize type throws an error of undefined.
I tried to use typeof to test condition of filesize is undefined but it does not work. Need help to catch error.
$('#fileupload-image').change(function() {
$('#select_file').hide();
$('#upload_image_button').slideDown('fast');
var filename = $(this).val();
if(typeof this.files[0].size == 'undefined')
{
this.files[0].size = '';
}
else
{
var filesize = this.files[0].size;
}
Share
Improve this question
edited Dec 16, 2015 at 21:16
TbWill4321
8,6863 gold badges29 silver badges25 bronze badges
asked Dec 16, 2015 at 21:15
Errol WallaceErrol Wallace
1672 silver badges13 bronze badges
3 Answers
Reset to default 3another possibility:
var filesize = (this.files.length && this.files[0].size) || '';
First of all, I don't think you actually want to be setting this.files[0].size = '';
.
Here's one possibility, using a try/catch to forget all about type checking:
var filesize = '';
try {
filesize = this.files[0].size;
}
catch {}
Use a this.files.length > 0 check instead of checking to see if it is undefined. Also, if you're checking to see if it's undefined you wouldn't want to be using single quotes either.
本文标签: javascriptTypeError cannot set property 39size of39 undefinedStack Overflow
版权声明:本文标题:javascript - TypeError cannot set property 'size of' undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744680659a2619385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论