0

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?

  • It will not help you, but first you open the file, *then* check whether it exists? – Hans Kesting Apr 27 '17 at 12:04
  • for mimetypes, see http://filext.com/faq/office_mime_types.php – Hans Kesting Apr 27 '17 at 12:04
  • Suggestion: Maximum path length in Windows OS is 255 characters. See if you have - I had this problem, that the path where the file was, was way over 255 characters. It downloaded the file, was there, but cannot open. – Calin Vlasin Apr 27 '17 at 12:06
  • @HansKesting yes, I've tried all the mimetypes related to powerpoint from that link before posting this question :) And the check after the file exists...that's just some leftover due to all the times I've tried to rewrite this method to get the file to be downloaded properly... :( – Proton Commander Apr 27 '17 at 12:07
  • @CalinVlasin Thanks for your comment: I just checked it and path is 99 characters, so it should be good. – Proton Commander Apr 27 '17 at 12:12
  • You can probably adjust this answer: http://stackoverflow.com/a/9549889/121309 – Hans Kesting Apr 27 '17 at 13:25
  • That approach was what I had at the very beginning. It will return a bin file with 33mb. Nevertheless, if I specify the Content-Type as "application/pptx" or "application/vnd.openxmlformats-officedocument.presentationml.presentation" it will return a pptx file that can't be opened (weighting 33mb too). The original file weighs 18mb so clearly something's off with the bytes. I've made sure position of streams are set to 0 too but same output. – Proton Commander Apr 27 '17 at 13:58

1 Answers1

1
          byte[] buffer = new byte[(int)stream.Length];
          result.Content = new ByteArrayContent(buffer);

This means that you have a buffer of the correct size, but empty. You still need to fill it from that stream.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111