最初的问题:我希望我未来的网络服务器的用户可以使用头像。 最初的解决方案(及其问题)
让用户上传自己的图片并保存
在这里我们提出了第二个想法:让用户定义并保存图像的链接,为他的头像
所以问题是:有没有办法向浏览器“说”当它看到:
<img src='http://external.jpg' ...>
external.jpg大小应该限制在100KB?如果它超过那个尺寸,那么就减掉它的载荷?
答案 0 :(得分:1)
使用此功能,您可以获取文件大小并确定从那里做什么。如果你想将它存储在你的硬盘上,你也可以使用像Clarifai这样的API来检查非法内容。
function get_filesize(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET",
// to get only the header
xhr.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(parseInt(xhr.getResponseHeader("Content-Length")));
}
};
xhr.send();
}
get_filesize("URLHERE", function(size) {
alert("The size of foo.exe is: " + size + " bytes.");
});
&#13;