You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Vimeo API的HEAD响应中获取Upload-Length和Upload-Offset?

解决Vimeo API上传进度追踪问题

首先,你调用progress.json()报错是因为HEAD请求的响应没有JSON体——Vimeo的TUS上传进度接口是通过响应头返回Upload-Offset和Upload-Length的,不是JSON数据。

修正getProgress函数

直接从响应头中提取所需字段即可,同时注意:跨域场景下,浏览器默认不会暴露自定义响应头,Vimeo的API已经配置允许Access-Control-Expose-Headers包含Upload-Offset和Upload-Length,无需额外配置。

修正后的代码:

async function getProgress(upload_link : string) {
    const response = await fetch(upload_link, {
        method: 'HEAD',
        headers: {
            'Tus-Resumable': '1.0.0'
            // 移除不必要的Accept头,HEAD请求不需要JSON格式响应
        },
    });

    if (!response.ok) {
        throw new Error(`获取进度失败: ${response.statusText}`);
    }

    // 从响应头提取进度信息
    const uploadOffset = parseInt(response.headers.get('Upload-Offset') || '0', 10);
    const uploadLength = parseInt(response.headers.get('Upload-Length') || '0', 10);
    
    console.log(`已上传: ${uploadOffset} / ${uploadLength} (${Math.round((uploadOffset/uploadLength)*100)}%)`);

    // 轮询追踪进度(添加间隔避免频繁请求)
    if (uploadOffset < uploadLength) {
        setTimeout(() => getProgress(upload_link), 1000); // 每秒查询一次
    } else {
        console.log('上传完成');
    }
}

额外注意事项

  1. Tus版本一致性:确保上传和进度查询的Tus-Resumable头版本完全一致(都是1.0.0),否则Vimeo会返回错误。
  2. 避免栈溢出:原代码直接递归调用getProgress会导致调用栈溢出,改用setTimeout做轮询更安全。
  3. 分块上传适配:如果视频是分块上传的(超过Vimeo单块限制),每次PATCH后Upload-Offset会自动更新,进度查询会同步获取最新偏移量。

内容的提问来源于stack exchange,提问作者jrfii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:55:17