views:

237

answers:

1

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  A: 

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.

sascha
That worked great. Thanks!
Mike Pateras

related questions