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

使用expo-file-system下载视频时出现Promise未处理拒绝错误的排查

问题描述

我使用expo-file-system下载视频,此前偶尔能成功但有时会报错,近期修改了部分内容(具体改动记不清)后,该错误持续出现。

前端代码如下:

const result = await FileSystem.createDownloadResumable(
    `http://192.168.0.12:5000${data.staticPath}`,
    vidDir + data.path,
    {},
    (downloadProgress) => {
        console.log(downloadProgress);
    }
).downloadAsync().catch(function(error) {
    console.log("Error: " + error.message);
    throw error;
});

下载进度显示即将完成时终止,报错信息:

LOG  {"totalBytesExpectedToWrite": 5632573, "totalBytesWritten": 5089880}
 LOG  Error: unexpected end of stream
 WARN  Possible Unhandled Promise Rejection (id: 2):
Error: unexpected end of stream
construct@[native code]
construct@[native code]
_construct@http://192.168.0.12:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:19240:28
Wrapper@http://192.168.0.12:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:19202:25
construct@[native code]
_createSuperInternal@http://192.168.0.12:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:94967:322
CodedError@http://192.168.0.12:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false:94975:26

后端采用Flask编写,代码仅返回待下载文件的静态路径:

@app.route("/requestVideo/<url>")
def requestVideo(url):
    path = vid_request.download(url, "static")
    
    info = {
         "path": path,
         "staticPath": url_for('static', filename=path)
    }

    return jsonify(info)
解决方案

1. 验证后端文件完整性

unexpected end of stream核心原因是下载的文件不完整,先检查后端static目录下的目标视频文件:

  • 对比文件字节数和原始文件是否一致
  • 尝试手动打开文件,确认是否损坏

2. 修复Flask静态文件服务逻辑

Flask默认static路由处理大文件时可能存在传输截断问题,建议手动实现文件流传输,支持断点续传:

from flask import send_file
import os

# 新增专门的文件下载接口
@app.route("/downloadVideo/<path:filename>")
def download_video(filename):
    file_path = os.path.join("static", filename)
    if not os.path.exists(file_path):
        return "File not found", 404
    # 启用条件请求和断点续传支持
    return send_file(file_path, conditional=True)

前端修改请求地址为该新接口,替代原static路径。

3. 优化前端下载配置

  • 在下载配置中禁用压缩,避免部分服务器处理异常:
const downloadTask = FileSystem.createDownloadResumable(
    `http://192.168.0.12:5000/downloadVideo/${data.path}`,
    vidDir + data.path,
    { headers: { 'Accept-Encoding': 'identity' } }, // 禁用gzip压缩
    (downloadProgress) => {
        console.log(downloadProgress);
    }
);
  • 添加失败重试逻辑,应对临时网络波动:
try {
    const result = await downloadTask.downloadAsync();
} catch (error) {
    console.log("Retry download...");
    // 重试一次下载任务
    const result = await downloadTask.downloadAsync();
}

4. 排查网络稳定性

确保测试设备和服务器处于同一稳定WiFi环境,避免局域网内信号弱、带宽不足导致的传输中断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 09:30:57