获取文件的大小,通过AJAX请求 [英] Get size of file requested via ajax

查看:97
本文介绍了获取文件的大小,通过AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面就是我希望做的:

我要发送一个Ajax请求的文件(preferably与jQuery),一旦文件被加载,确定所需的文件的大小。

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.

在一点谷歌搜索的,很显然,我甚至不具备正确的问题是一个好主意问摸不着头脑。任何帮助将大大AP preciated。

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.

推荐答案

您可以做一个HTTP的 HEAD 请求,并通过阅读内容 - 获取文件的大小近似长度 HTTP头。

You could make a HTTP HEAD request, and get a file size approximate by reading the Content-Length HTTP Header.

此类型的请求被用于获得关于由请求隐含的URL的元信息,不转移的任何内容中的响应

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.

var xhr = $.ajax({
  type: "HEAD",
  url: "path/to/file.ext",
  success: function(msg){
    alert(xhr.getResponseHeader('Content-Length') + ' bytes');
  }
});

这篇关于获取文件的大小,通过AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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