I use this code to return a FileContentResult with an MSI file to be downloaded by a user in my 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"); }
I can download the file, but when I try to run the installer, I get an error message:
This installation package cannot be opened. Contact your application vendor to verify that this is a valid Windows Installer package.
I know that the problem is not in C: \ WixTest.msi, because it works fine if I use a local copy. I donβt think I am using the wrong MIME type because I can get something like this just using File.Copy and returning the copied file via FilePathResult (without using StreamReader), which works correctly after loading.
I need to use FileContentResult so that I can delete a copy of the file that I create (which I can do as soon as I load it into memory).
I think I invalidate the installation package by copying or encoding the file. Is there a way to read the MSI file in memory and return it using FileContentResult without damaging the installation package?
Decision:
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"); }
source share