Search code examples
htmlhttpcontent-disposition

Where is My files actual data http content-disposition?


I am trying to understand how downloads are sent to the clients browser using reverse engineering of code.

What I have so far discovered is that HTTP headers play a large role particularly in reference to the Content-Disposition tag / line. For example, I have found out that the following code sets up a download for a txt file named TEST1234, and that it is downloaded as an attachment as apposed to an inline.

HTTP/1.x 200 OK
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=" ,"TEST1234.txt

However I was expecting that the code would also hold the files actual contents with in the rest of the http or at least the ajoining HTML code?

What leads me to think this is because some PHP codes are as follows...

<?php
  $filename="download.txt";
  header("Content-disposition: attachment;filename=$filename");
  readfile($filename);
?>

I am not at all familiar with PHP but can make out enough to see that the line readfile($filename); probably reads the file (server end) into a stream and adds the files coding it to either the HTTP header or the adjoining HTML that is posted as response to the client? Upon receiving the response, the clients browser knows that the file should be saved as "download".txt. Assuming that download.txt contained the text Hello world, I would think the sent response would look as follows?

    HTTP/1.x 200 OK
    Content-Type: application/octet-stream
    Content-Disposition: attachment; filename=" ,"download.txt"

    <html>Hello world</html>

Solution

  • I have found the solution...

    I had simply not created a blank line between the HTTP header and the HTML.... or in this case the file contents...

                    .Append("HTTP/1.1 200 OK" & Environment.NewLine)
                    .Append("Content-Type: text/plain" & Environment.NewLine)
                    .Append("Content-Disposition: attachment; filename=" & Chr(34) & "TEST.txt" & Chr(34) & Environment.NewLine)
                    .Append(Environment.NewLine)    <--------------- THIS LINE NOW WORKS
                    .Append("Hello world!")
    

    Although is there an way to encode web freindly/ safe encode the contents which the browser knows how to decode back to its original contents... simular to base64 encoding?