使用 jQuery 查找下载链接后面的文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1440723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:27:23  来源:igfitidea点击:

Find size of file behind download link with jQuery

jqueryhtmlfilesize

提问by Seedorf

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.

我想知道是否有办法使用 jQuery 找出我链接到网页的 PDF 的文件大小。

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

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

Cheers in advance.

提前加油。

回答by masto

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.

我觉得在客户端做这件事太多了,但我不知道你的应用程序的细节,所以假设你必须用 jQuery 来做......你可以做一个 HEAD 请求并查看内容 -长度标题。之前我推荐了jqHead插件,但是尝试使用后发现它坏了。

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

这是一个不依赖任何额外插件的工作示例: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"));
    }
  });

回答by Sekar Ramamurthy

Here is working snippet to find the file size in javascript without going to the server, if you are about to upload the file to the server.

如果您要将文件上传到服务器,这里是工作片段,可在不访问服务器的情况下在 javascript 中查找文件大小。

This idea helps to restrict the file size before uploading big files to the server.

这个想法有助于在将大文件上传到服务器之前限制文件大小。

<input type="file" id="loadfile" />
<input type="button" value="find size" onclick="findSize()" />
<script type="text/javascript">
    function findSize() {
        var fileInput = $("#loadfile")[0];
        alert(fileInput.files[0].size); // Size returned in bytes.
    }    
</script>

回答by Saeven

In the answer above, Chrome 21 and Safari 6 did not have a fileSize member, but does have size. Use:

在上面的答案中,Chrome 21 和 Safari 6 没有 fileSize 成员,但有大小。用:

fileInput.files[0].size

文件输入.文件[0].大小

回答by Chetan Sastry

If you know the file size beforehand, you can put that in the title attribute of the download link. You can also use jQuery to show a custom popup.

如果你事先知道文件大小,你可以把它放在下载链接的标题属性中。您还可以使用 jQuery 来显示自定义弹出窗口。

Otherwise, there is no way to get just the file size (content-length) of a URL without actually downloading the whole content AFAIK.

否则,如果不实际下载整个内容 AFAIK,就无法仅获取 URL 的文件大小(内容长度)。

回答by Corey Sunwold

The only way that I can think of to do this would be to create a web service that would return you the filesize of the PDF.

我能想到的唯一方法是创建一个 Web 服务,该服务将返回 PDF 的文件大小。