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

DeepL API C#库:TranslateDocumentAsync无法传入FileStream的问题咨询

解决DeepL API C#库中Stream参数传入TranslateDocumentAsync的报错问题

报错提示Cannot convert from System.IO.FileStream to System.IO.FileInfo,本质是编译器未匹配到接受Stream类型的重载方法,而是错误尝试匹配FileInfo版本的重载,导致类型转换失败。以下是具体解决步骤:

  • 显式指定参数名称消除重载歧义
    通过命名参数明确告诉编译器使用Stream版本的重载,避免参数匹配错误:

    await _translator.TranslateDocumentAsync(
        sourceStream: sourceFileStream,
        targetStream: targetFileStream,
        targetLanguage: "FR",
        sourceLanguage: "DE");
    

    注意:Stream重载的参数顺序是源流→目标流→目标语言→源语言,和你原代码的参数顺序一致,但命名参数能强制编译器匹配正确的重载。

  • 必要时指定源文件名识别文件格式
    如果DeepL无法通过流自动识别文件类型,需额外传入sourceFileName参数(带扩展名,如"report.pdf"),帮助API判断文件格式:

    await _translator.TranslateDocumentAsync(
        sourceStream: sourceFileStream,
        targetStream: targetFileStream,
        targetLanguage: "FR",
        sourceLanguage: "DE",
        sourceFileName: "your-source-file.docx");
    
  • 使用using语句管理流资源
    确保FileStream被正确释放,避免资源泄漏:

    using var sourceStream = File.OpenRead("path/to/your/source/file");
    using var targetStream = File.Create("path/to/your/target/file");
    
    await _translator.TranslateDocumentAsync(
        sourceStream: sourceStream,
        targetStream: targetStream,
        targetLanguage: "FR",
        sourceLanguage: "DE");
    
  • 检查并升级DeepL库版本
    旧版本的DeepL .NET库可能没有Stream类型的重载,通过NuGet包管理器升级DeepL包到最新版即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 13:57:40