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

时间:2023-02-06 00:29:12

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.

提前欢呼。

5 个解决方案

#1


46  

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请求并查看Content-Length标题。之前我推荐了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"));
    }
  });

#2


12  

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>

#3


0  

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.

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

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

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

#4


0  

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的文件大小。

#5


0  

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

fileInput.files[0].size

#1


46  

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请求并查看Content-Length标题。之前我推荐了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"));
    }
  });

#2


12  

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>

#3


0  

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.

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

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

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

#4


0  

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的文件大小。

#5


0  

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

fileInput.files[0].size