我使用此代码返回一个带有 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/