The PowerPoint file in question is 18mb in size. After clicking on a button, the following GET method in a controller is called:
[Route("api/download/GetFile")]
[HttpGet]
public HttpResponseMessage GetFile()
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
try
{
var localFilePath = HttpRuntime.AppDomainAppPath + "content\\files\\file.pptx";
var stream = File.OpenRead(localFilePath);
stream.Position = 0;
stream.Flush();
if (!System.IO.File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
byte[] buffer = new byte[(int)stream.Length];
result.Content = new ByteArrayContent(buffer);
result.Content.Headers.Add("Content-Type", "application/pptx");
result.Content.Headers.Add("x-filename", "file.pptx");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.Add("Content-Length", stream.Length.ToString());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.presentationml.presentation");
}
return result;
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
The file downloads fine through the browser to the downloads folder and the size is exactly the same as the original file that is being called for download on the server. However, when trying to open it:
"PowerPoint found unreadable content in file.pptx. Do you want to recover the contents of this presentation? If you trust the source of this presentation, click Yes."
Clicking "yes" only makes PowerPoint load for a while and then return error message "There was a problem accessing this file".
I suspect the issue lies within this "x-filename" but changing this value to something else ends up making the browser download a bin file with just a few KB. I've also tried changing ContentType and MediaTypeHeaderValue to a lot of different things (application/pptx, application/x-mspowerpoint, etc...) but nothing does the trick. Additionally I've also tried to assign the Content like this:
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
which also doesn't work.
Any help appreciated.
EDIT:
As Hans Kesting pointed out, it seems that I wasn't copying the bytes properly. However, doing the below still produces same error message when trying to open the powerpoint file, but the file now is 33mb in size:
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
byte[] buffer = memoryStream.ToArray(); // stream.CopyTo// new byte[(int)stream.Length];
result.Content = new ByteArrayContent(buffer);
How come when I was copying the bytes in the wrong way the downloaded file had the same exact amount of bytes as the original file but now it has 33 mb?