javascript - TypeError cannot set property 'size of' undefined - Stack Overflow

Trying to get the size of a file when the user uploads file.When user clicks on the browse button to u

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
Add a ment  | 

3 Answers 3

Reset to default 3

another 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744680660a4587602.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>