ASP.NET MVC:可以通过 FileContentResult 返回 MSI 文件而不破坏安装包吗?
我使用此代码返回带有 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");
}
I'm using this code to return a FileContentResult with an MSI file for the user to download in my 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");
}
I can download the file, but when I try to run the installer I get an error message saying:
This installation package could not be
opened. Contact the application vendor
to verify that this is a valid Windows
Installer package.
I know the problem isn't C:\WixTest.msi, because it runs just fine if I use the local copy. I don't think I'm using the wrong MIME type, because I can get something similar with just using File.Copy and returning the copied file via a FilePathResult (without using a StreamReader) that does run properly after download.
I need to use the FileContentResult, however, so that I can delete the copy of the file that I'm making (which I can do once I've loaded it into memory).
I'm thinking I'm invalidating the install package by copying or encoding the file. Is there a way to read an MSI file into memory, and to return it via a FileContentResult without corrupting the install package?
Solution:
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用二进制编码和内容类型
application/msi
而不是text/plain
- 它不是 ASCII 或文本内容,因此您会破坏文件。Try using binary encoding and content-type
application/msi
instead oftext/plain
- it's not ASCII or text content so you're mangling the file.