【问题标题】:Download blob data as file in MVC在 MVC 中将 blob 数据下载为文件
【发布时间】:2017-06-27 14:24:34
【问题描述】:

我有一个文件 testme.txt,其中包含“test test test test”作为文本,我正在使用 Oracle 将文件作为 blob 检索,并且我正在使用 byte[] 来存储数据。 我从 Oracle 获得的数据是“74657374207465737420746573742074657374”。当我尝试下载这个文件时,下载的文件有一些奇怪的符号。 我试过的代码是

public ActionResult DownloadFiles(string fileSeq)
    {            
        var byteArray=byte_array_got_from_oracle;
        var stream = new MemoryStream(byteArray);
        return new FileStreamResult(stream, MediaTypeNames.Application.Octet); //since the file could be of any type
    }

我尝试了各种方法,似乎都没有下载带有原始数据的文本文件。

【问题讨论】:

  • 你是怎么得到byte_array_got_from_oracle的?
  • @mason:有一个 Oracle 包函数,它以 xml 格式返回数据,我将其转换为类对象
  • XML?看,这听起来不对。这就是为什么您需要在问题中包含MCVE 的原因。你的问题不完整。

标签: c# arrays download memorystream


【解决方案1】:

试试这个解决方案:

    public ActionResult DownloadTest()
    {
        var data = new byte[] { 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74 };


        return File(data, MediaTypeNames.Application.Octet);
    }

并且下载的文件包含:

    test test test test

【讨论】:

  • 我拥有的字节数组的值类似于“249, 142, 185, ..”。我什至不知道这些值是什么
  • 没关系,解决方法是使用File(byte_array_got_from_oracle, MediaTypeNames.Application.Octet)方法而不是FileStreamResult
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2012-03-13
  • 2021-08-09
  • 2020-11-17
相关资源
最近更新 更多