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

孤者浪人 提交于 2019-12-23 11:59:17

问题


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");
}

回答1:


Try using binary encoding and content-type application/msi instead of text/plain - it's not ASCII or text content so you're mangling the file.



来源:https://stackoverflow.com/questions/2382352/asp-net-mvc-can-an-msi-file-be-returned-via-a-filecontentresult-without-breakin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!