使用jQuery查找下载链接后的文件大小 [英] Find size of file behind download link with jQuery

查看:132
本文介绍了使用jQuery查找下载链接后的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用jQuery来查找我链接到网页的PDF文件大小。

I was wondering if there was a way to use jQuery to find out the file size for a PDF that i'm linking to a webpage.

我想让它在悬停在下载链接上时弹出一个模式框,指出PDF的文件大小。我可以做第二点,我唯一想知道的是如何找出文件大小。我不知道jQuery是否可以做到这一点。如果不是,那么我猜得太糟糕了。

I want to make it so that on hovering over the download link, a modal box pops up saying the file size of the PDF. I can do the second bit, the only thing i'd like to know is how to find out the file size. I dont know if jQuery is capable of this. If not then too bad i guess..

提前喝彩。

Cheers in advance.

推荐答案

我觉得这样做太过于客户端,但我不知道应用程序的细节,因此假定您必须使用jQuery来完成它...您可以执行HEAD请求并查看Content-Length标头。之前我推荐过jqHead插件,但在尝试使用它之后,我发现它已被破坏。

I feel like this is too much to be doing client-side, but I don't know the details of your application, so presuming you have to do it with jQuery... you can do a HEAD request and look at the Content-Length header. Earlier I recommended the jqHead plugin, but after I tried to use it I found that it was broken.

下面是一个工作示例,它不依赖于任何额外的插件: http://host.masto.com/~chris/so/jqhead.html

Here's a working example that doesn't depend on any extra plugins: http://host.masto.com/~chris/so/jqhead.html

这很简单:

The meat of it is very simple:

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

这篇关于使用jQuery查找下载链接后的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆