ASP.NET MVC:可MSI文件可以通过一个FileContentResult不破安装包退换? [英] ASP.NET MVC: Can an MSI file be returned via a FileContentResult without breaking the install package?
问题描述
我使用这个code返回FileContentResult与用户MSI文件在我的 ASP.NET MVC 控制器:
使用(StreamReader的读者=新的StreamReader(@C:\\ WixTest.msi))
{
字节[]字节= Encoding.ASCII.GetBytes(reader.ReadToEnd()); 返回文件(字节,text / plain的,download.msi);
}
我可以下载文件,但是当我尝试运行安装程序,我得到一个错误消息说:
此安装包不能
打开。联系应用程序供应商
以验证这是一个有效的Windows
安装程序包。
块引用>我知道这个问题是不是C:\\ WixTest.msi,因为如果我使用本地副本运行良好。我不认为我使用了错误的MIME类型,因为我可以得到只使用File.Copy并通过FilePathResult返回复制的文件(不使用一个StreamReader)类似的东西,它下载后运行正常。
我需要使用FileContentResult,但是,这样我可以删除我使文件(这一次,我已经加载到内存中我可以做)的副本。
我想我通过复制或编码文件无效的安装包。有没有办法读取MSI文件到内存中,并通过FileContentResult而不会破坏安装包退货吗?
解决方案:
使用(的FileStream流=新的FileStream(@C:\\ WixTest.msi,FileMode.Open))
{
BinaryReader读卡器=新BinaryReader(流);
字节[]字节= reader.ReadBytes(Convert.ToInt32(stream.Length)); 返回文件(字节,应用程序/ MSI,download.msi);
}
解决方案尝试使用二进制编码和内容类型
应用程序/ MSI
而不是正文/纯
- 这不是ASCII或文本内容,以便你重整文件。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"); }
解决方案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.这篇关于ASP.NET MVC:可MSI文件可以通过一个FileContentResult不破安装包退换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!