Javascript check file size

前端 未结 4 1668
时光取名叫无心
时光取名叫无心 2021-01-12 22:33

Is it possible to keep checking with javascript if the filesize of a file on a webserver (e.g. http://www.mysite.com/myfile.js) is larger than 0 bytes and if so return a tru

4条回答
  •  天命终不由人
    2021-01-12 23:00

    The idea given by josh3736 is great. Only the code example he gave, refused to work in my browser (Chrome 20, Firefox 13, IE 8, Opera 12), for the reason, that I don't know.

    Here is the one, that worked perfectly in my case:

    jQuery.ajax
    ({
        cache: false,
        type: 'HEAD',
        url: 'myfile.js',
        success: function(d,r,xhr){alert('File size is ' + xhr.getResponseHeader('Content-Length') + ' bytes.')},
        error: function(xhr, desc, er){alert('ERROR: "' + xhr.responseText + '"')}
    });
    

    I want also to notice, that Apache on-board XAMPP server works just fine with HEAD request, but of course, when run on localhost, such request is blocked by a browser with error message: "Origin localhost is not allowed by Access-Control-Allow-Origin" (example from Chrome).

提交回复
热议问题