通过ajax获取请求的文件大小
这就是我希望做的事情:
我想向文件发送 ajax 请求(最好使用 jQuery),一旦文件加载,就确定所请求文件的大小。
经过一番谷歌搜索后,很明显,我什至不知道要问什么问题才能解决这个问题。任何帮助将不胜感激。
Here's what I'm hoping to do:
I want to send an ajax request to a file (preferably with jQuery), and once the file has been loaded, determine the size of the requested file.
After a bit of googling, it's clear that I don't even have a good idea of the right question to ask to figure this out. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以发出 HTTP HEAD 请求,并获得通过读取 Content-Length HTTP 标头来估算文件大小。
这种请求用于获取有关请求隐含的 URL 的元信息,而不在响应中传输其任何内容。
You could make a HTTP HEAD request, and get a file size approximate by reading the Content-Length HTTP Header.
This kind of request is used to obtain meta-information about the URL implied by the request, without transferring any content of it in the response.
您可以创建一个服务器端命令,该命令获取文件的路径和名称,打开文件以获取长度,然后将大小发送回客户端。如何操作取决于您使用的服务器端语言。这是您正在寻找的行动类型吗?
You could create a server-side command that takes the path and name of the file, opens the file to get the length, and sends the size back to the client. How you do it would depend on what server-side language you're using. Is that the type of action you're looking for?
您基本上会执行 HEAD HTTP 请求并查询 Content-Length,这会返回 X 字节。
You basically do a HEAD HTTP request and query the Content-Length, which returns X bytes.