AFP collects all data to be sent back to the browser in a buffer. This buffer is limited to the maximum length of a character string in Visual FoxPro – at the time of this writing just under 16 MB. Use Response.BinaryWrite() to add this character string to the buffer without any kind of conversion.
Syntax
Response.BinaryWrite( cData )
Parameter
cData
Generally you use AFP to create HTML documents that do not include binary data. It is not possible e.g. to integrate an image directly in an HTML document. Returning other document types as HTML allows you to return any data to the client through Response.BinaryWrite. The data contained in cData will be added to the result buffer without code page conversion.
AFP documents may return content of any kind. If, however, the data to be returned is not HTML data, you will have to set the attribute Response.ContentType to the correct MIME type. Commonly used MIME types are listed in the documentation to Response.ContentType.
If e.g. you want to generate PDF reports in an application, you would not want to deposit this PDF file in a freely accessible directory. If you did, the web server would on its own make sure to provide the data; on the other hand you would not be able to make sure that users received only their own reports. You would also have to delete the reports in the application yourself.
Using AFP, you can obtain a temporary file through Session.GetSessionFileName() that will be deleted by AFP as soon as the session expires. Users don't have direct access to the session directory. That is why you need an AFP page that returns a PDF document:
<%
lcPdfFile = Session.GetSessionFileName("PDF_Document")
Response.Reset()
Response.ContentType = "application/pdf"
Response.BinaryWrite(lcPDFFile)
%>
In AFP documents, anything not inside the characters <%...%> is always added to the output buffer. If as in this case you want to return binary data, you should make sure not to inadvertently return additional characters as these would distort the result. In the sample above, %> should not be followed by a final line break. If you press CTRL+END inside an editor, the cursor should be positioned directly after %>. If you consider outputs of binary data through .afp documents too risky, you can also create .afp documents with a size of 0 bytes. In the file .afp.code you output the file in Event_PageBefore or in Event_PageAfter.
Note
At the moment, the only difference between Response.Write() and Response.BinaryWrite() is that in Response.BinaryWrite() data will not be converted to character strings. There is no need to call the Visual FoxPro function TRANSFORM(). Future versions of AFP could support different code pages and character encoding like Unicode etc. In that case Response.Write() would effect the appropriate conversion while Response.BinaryWrite() would still add the data unchanged and in its exact byte length to the output buffer.
See also
Response.ContentType | Response.Reset() | Response.Write()