使用 jQuery 查找下载链接后面的文件大小
我想知道是否有一种方法可以使用 jQuery 来找出我链接到网页的 PDF 的文件大小。
我想让鼠标悬停在下载链接上时,会弹出一个模式框,显示 PDF 的文件大小。我可以做第二位,我唯一想知道的是如何找出文件大小。我不知道 jQuery 是否有能力做到这一点。如果不是那么我想就太糟糕了..
提前干杯。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我觉得这对于客户端来说太多了,但我不知道你的应用程序的详细信息,所以假设你必须使用 jQuery 来完成它......你可以做一个 HEAD 请求并查看内容 -长度标头。早些时候我推荐了jqHead插件,但是当我尝试使用它之后我发现它被破坏了。
这是一个不依赖于任何额外插件的工作示例:http://host. masto.com/~chris/so/jqhead.html
它的核心内容非常简单:
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.
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:
如果您要将文件上传到服务器,这里是在 javascript 中查找文件大小的工作片段,无需访问服务器。
这个想法有助于在将大文件上传到服务器之前限制文件大小。
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.
This idea helps to restrict the file size before uploading big files to the server.
如果您事先知道文件大小,则可以将其放入下载链接的标题属性中。您还可以使用 jQuery 显示自定义弹出窗口。
否则,没有办法在不实际下载整个内容的情况下仅获取 URL 的文件大小(内容长度)。
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.
Otherwise, there is no way to get just the file size (content-length) of a URL without actually downloading the whole content AFAIK.
我能想到的唯一方法是创建一个 Web 服务来返回 PDF 的文件大小。
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.
在上面的答案中,Chrome 21 和 Safari 6 没有 fileSize 成员,但有大小。使用:
fileInput.files[0].size
In the answer above, Chrome 21 and Safari 6 did not have a fileSize member, but does have size. Use:
fileInput.files[0].size