asp.net-mvc-2 - ASP.NET MVC : Can an MSI file be returned via a FileContentResult without breaking the install package?

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

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

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

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

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

This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package.

我知道问题不在于 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");
}

最佳答案

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

关于asp.net-mvc-2 - ASP.NET MVC : Can an MSI file be returned via a FileContentResult without breaking the install package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2382352/

相关文章:

asp.net-mvc - 为使用 ASP.NET MVC 2 AsyncController 实现的长时间运行的任务实现进度条

c# - 如何使用 StreamReader(或其他替代方法)在 IOS 应用程序中的 Xamerin 上将文件读入数组

go - 使用 io.Pipe() 读/写 golang 下载 zip 文件

c# - 如何在 .NET 中读取大型 (1GB) 文本文件?

jquery - ASP.NET MVC : Send back HTML inside Json object

asp.net-mvc - ASP.NET MVC Html.TextBoxFor呈现的值与<%: Model.值%>不同

JQueryidleTimeout插件: How to display the dialog after the session is timedout on ASP. NET MVC页面

installation - 升级时保留注册表设置

windows-installer - 是否可以从 Installshield Express 创建独立的 MSI?

windows - 使用 WiX 生成和安装 Typelib