我使用此代码返回FileContentResult与MSI文件,为用户在我ASP.NET MVC控制器下载:ASP.NET MVC:MSI文件可以通过FileContentResult返回而不破坏安装包吗?
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");
}
这很好。谢谢! –