Can I get an image's file size and mime-type via JavaScript (FireFox/Gecko/Mozilla only)

岁酱吖の 提交于 2019-12-10 11:38:09

问题


Can I get an image's file size and mime-type via JavaScript. I am writing a script for inspecting all images in a document. I will run the script in FireFox only so a solution specific to FireFox (undocumented/non-standard/otherwise) is perfectly acceptable. I can use jQuery if necessary.


回答1:


You could make a HEAD request with XHR which will tell you the file size, provided these images are on your domain.




回答2:


you can do it through HTTP head request

       var request;
        request = $.ajax({
                  type: "HEAD",
                  url: 'your image url',
                  success: function () {
                  alert("Size is " + request.getResponseHeader("Content-Length"));
                  alert("Type is " + request.getResponseHeader("Content-Type"));
                  }
               });


来源:https://stackoverflow.com/questions/5456245/can-i-get-an-images-file-size-and-mime-type-via-javascript-firefox-gecko-mozil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!