【问题标题】:ASP.NET MVC: Can an MSI file be returned via a FileContentResult without breaking the install package?ASP.NET MVC:可以通过 FileContentResult 返回 MSI 文件而不破坏安装包吗?
【发布时间】:2010-03-04 20:06:41
【问题描述】:

我正在使用此代码返回一个带有 MSI 文件的 FileContentResult,供用户在我的 ASP.NET MVC 控制器中下载:

using (StreamReader reader = new StreamReader(@"c:\WixTest.msi"))
{
    Byte[] bytes = Encoding.ASCII.GetBytes(reader.ReadToEnd());

    return File(bytes, "text/plain", "download.msi");
}

我可以下载文件,但是当我尝试运行安装程序时,我收到一条错误消息:

这个安装包不能 打开。联系应用程序供应商 验证这是一个有效的 Windows 安装包。

我知道问题不在于 C:\WixTest.msi,因为如果我使用本地副本,它运行得很好。我不认为我使用了错误的 MIME 类型,因为我可以通过仅使用 File.Copy 并通过下载后正常运行的 FilePathResult(不使用 StreamReader)返回复制的文件来获得类似的结果。

但是,我需要使用 FileContentResult,这样我才能删除正在制作的文件的副本(一旦我将其加载到内存中就可以这样做)。

我认为我通过复制或编码文件来使安装包无效。有没有办法将 MSI 文件读入内存,并通过 FileContentResult 返回而不损坏安装包?

解决方案:

using (FileStream stream = new FileStream(@"c:\WixTest.msi", FileMode.Open))
{
    BinaryReader reader = new BinaryReader(stream);
    Byte[] bytes = reader.ReadBytes(Convert.ToInt32(stream.Length));

    return File(bytes, "application/msi", "download.msi");
}

【问题讨论】:

    标签: asp.net-mvc-2 windows-installer streamreader filecontentresult


    【解决方案1】:

    尝试使用二进制编码和内容类型 application/msi 而不是 text/plain - 它不是 ASCII 或文本内容,因此您正在修改文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多