开发者

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

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开发者_StackOverflow中文版 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 of text/plain - it's not ASCII or text content so you're mangling the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜