phpslimx-sendfileslim-3

How to view files sent through x-sendfile as GET response


I am using Slim Framework 3. Here's my code:

....
    $response = $response->withStatus(200)
                        ->withHeader('X-Sendfile', $path)
                        ->withHeader('Content-Type', $contentType)
                        ->withHeader('Content-disposition:', 'attachment; filename=' . $fileName);
                    return $response;
....

When using through postman, I see the image. But, how do I show the image / video / audio on an html page using the response?


Solution

  • As the whole file is returned by x-sendfile, the file is actually a blob. Hence I set the responseType of my ajax request to blob in my javascript. I following the answer by @AdamZ here at Using Javascript to Display Blob and everything worked.