Javascript 如何在nodejs中使用url获取文件大小
我在url中有一些图像。我可以得到文件属性,属性包括图像的宽度和高度。我想获得字节大小 我试图使用如下所示的fs模块获取大小,但它不适用于url,尽管它适用于本地文件夹中的文件路径Javascript 如何在nodejs中使用url获取文件大小,javascript,node.js,keystonejs,Javascript,Node.js,Keystonejs,我在url中有一些图像。我可以得到文件属性,属性包括图像的宽度和高度。我想获得字节大小 我试图使用如下所示的fs模块获取大小,但它不适用于url,尽管它适用于本地文件夹中的文件路径 var stats = fs.statSync(url); var fileSizeInBytes = stats["size"] 您必须使用request或http。您可以通过发送头请求并检查内容长度字段来获取文件大小(它不会在每台服务器上都起作用): 使用curl: curl -I "https://cdnj
var stats = fs.statSync(url);
var fileSizeInBytes = stats["size"]
您必须使用
request
或http
。您可以通过发送头
请求并检查内容长度
字段来获取文件大小(它不会在每台服务器上都起作用):
使用
curl
:
curl -I "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js"
你得到的答复是:
HTTP/1.1 200 OK
Date: Tue, 03 Apr 2018 14:30:16 GMT
Content-Type: application/javascript; charset=utf-8
Content-Length: 9068
Connection: keep-alive
Last-Modified: Wed, 28 Feb 2018 04:16:30 GMT
ETag: "5a962d1e-236c"
Expires: Sun, 24 Mar 2019 14:30:16 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Accept-Ranges: bytes
Strict-Transport-Security: max-age=15780000; includeSubDomains
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 405c3b6e1911a8db-CDG
{
date: 'Tue, 03 Apr 2018 14:29:32 GMT',
'content-type': 'application/javascript; charset=utf-8',
'content-length': '9068',
connection: 'close',
'last-modified': 'Wed, 28 Feb 2018 04:16:30 GMT',
etag: '"5a962d1e-236c"',
expires: 'Sun, 24 Mar 2019 14:29:32 GMT',
'cache-control': 'public, max-age=30672000',
'access-control-allow-origin': '*',
'cf-cache-status': 'HIT',
'accept-ranges': 'bytes',
'strict-transport-security': 'max-age=15780000; includeSubDomains',
'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
server: 'cloudflare',
'cf-ray': '405c3a5cba7a68ba-CDG'
}
使用
请求
模块:
var request = require("request");
request({
url: "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js",
method: "HEAD"
}, function(err, response, body) {
console.log(response.headers);
process.exit(0);
});
你得到的答复是:
HTTP/1.1 200 OK
Date: Tue, 03 Apr 2018 14:30:16 GMT
Content-Type: application/javascript; charset=utf-8
Content-Length: 9068
Connection: keep-alive
Last-Modified: Wed, 28 Feb 2018 04:16:30 GMT
ETag: "5a962d1e-236c"
Expires: Sun, 24 Mar 2019 14:30:16 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Accept-Ranges: bytes
Strict-Transport-Security: max-age=15780000; includeSubDomains
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 405c3b6e1911a8db-CDG
{
date: 'Tue, 03 Apr 2018 14:29:32 GMT',
'content-type': 'application/javascript; charset=utf-8',
'content-length': '9068',
connection: 'close',
'last-modified': 'Wed, 28 Feb 2018 04:16:30 GMT',
etag: '"5a962d1e-236c"',
expires: 'Sun, 24 Mar 2019 14:29:32 GMT',
'cache-control': 'public, max-age=30672000',
'access-control-allow-origin': '*',
'cf-cache-status': 'HIT',
'accept-ranges': 'bytes',
'strict-transport-security': 'max-age=15780000; includeSubDomains',
'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
server: 'cloudflare',
'cf-ray': '405c3a5cba7a68ba-CDG'
}
将图像下载到您的临时
/tmp/
目录,然后将其视为常规文件
const request=require(“请求”),
fs=要求(“fs”),
远程url=”https://example.com/image.jpg",
path='/tmp/media.jpg',
媒体=请求(远程url).pipe(fs.createWriteStream(路径));
媒体。在(“完成”时,()=>{
返回fs.statSync(path).size;
});
如果您没有请求本地资源,则需要使用http
或https
模块来请求这些资源。除了@zzzzBov,请记住,如果服务器配置不正确,可能需要下载整个文件以获得文件大小。并非每台服务器都返回http头中的内容长度。因此,获取远程文件大小并不是最好的主意……只有在服务器返回内容长度时才有效。不是一个安全的aproach。是的,它是指定的,这取决于他是否知道他的目标服务器不是你的aproach下载整个文件,而不仅仅是标题?!不完全确定…您可能希望使用request.head
,这样它就不会下载整个文件。