【问题标题】:React Download Json File from ControllerReact 从控制器下载 Json 文件
【发布时间】:2022-01-12 16:28:07
【问题描述】:

我有一个 react 应用程序,它与后端 dotnet 控制器联系以下载各种类型的文件。当我下载大多数文件时,一切正常并且文件正确。当我下载一个 json 文件时,该文件只包含[object Object]。下面是调用方法中的代码。

  public downloadFile = async (fileId: number) => {
    const response = await HttpUtility.postFileDownload<any>(DOWNLOAD_URL + fileId, {})

    let fileName = response.headers['content-disposition'].split('filename=')[1].split('\'')[2]

    if (fileName === undefined) {
      fileName = `file-id-${fileId}-${moment().format()}`
    }

    fileDownload(response.data, fileName, response.headers['content-type'])
  }

当我查看 Chrome 中的开发工具时,响应看起来是正确的。关于我需要做什么来纠正这个问题的任何指示?

【问题讨论】:

    标签: reactjs json .net download


    【解决方案1】:

    这个五月有效

    fileDownload(JSON.stringify(response.data), fileName, response.headers['content-type'])
    

    【讨论】:

    • 谢谢。 Thant 工作。
    猜你喜欢
    • 2011-08-06
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多